how to change json key:value

后端 未结 4 1004
面向向阳花
面向向阳花 2020-12-29 00:01
//my json data    
var jsndata = \"{ \"id\": \"5001\", \"type\": \"None\" },
            { \"id\": \"5002\", \"type\": \"Glazed\" },
            { \"id\": \"5005\",          


        
相关标签:
4条回答
  • 2020-12-29 00:08
    <script>
    var json = [{ "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" },
                { "id": "5009", "type": "Juice" }];
    /**
     * The function searches over the array by certain field value,
     * and replaces occurences with the parameter provided.
     *
     * @param string field Name of the object field to compare
     * @param string oldvalue Value to compare against
     * @param string newvalue Value to replace mathes with
     */
    function replaceByValue( field, oldvalue, newvalue ) {
        for( var k = 0; k < json.length; ++k ) {
            if( oldvalue == json[k][field] ) {
                json[k][field] = newvalue ;
            }
        }
        return json;
    }
    
    /**
     * Let's test
     */
    console.log(json);
    
    replaceByValue('id','5001','5010')
    console.log(json);
    
    replaceByValue('type','Chocolate','only water')
    console.log(json);
    </script>
    
    0 讨论(0)
  • 2020-12-29 00:14

    modified the function from above to be able to change all values of a key,and increment it by 1. And you can pass in the jsonObj

    function replaceByValue( jsonObj, field, oldvalue, newvalue ) {
        for( var k = 0; k < jsonObj.length; ++k ) {
                 jsonObj[k][field] = (newvalue *1)+k;
    
        }
        return jsonObj;
    }
    

    //example

    var json = [{ "id": "5001", "type": "None" },
                    { "id": "5002", "type": "Glazed" },
                    { "id": "5005", "type": "Sugar" },
                    { "id": "5003", "type": "Chocolate" },
                    { "id": "5004", "type": "Maple" },
                    { "id": "5009", "type": "Juice" }]; 
    
    
    json; 
    
    replaceByValue( json, "id", "na", 123 );
    
    json;
    
    0 讨论(0)
  • 2020-12-29 00:31

    Take a look at Pinch, a (multi) data replacement tool for JavaScript objects/JSON. Here is a brief example how pinch.js could be used in your case:

    var data = [
      {
        id: 5001,
        type: 'None'
      },
      {
        id: 5002,
        type: 'Glazed'
      },
      {
        id: 5005,
        type: 'Sugar'
      },
      {
        id: 5003,
        type: 'Chocolate'
      },
      {
        id: 5004,
        type: 'Maple'
      },
      {
        id: 5009,
        type: 'Juice'
      }
    ];
    
    pinch(data, '/id/', function(path, key, value) {
      return (value === 5001) ? 5010 : value;
    });
    
    0 讨论(0)
  • 2020-12-29 00:33

    try this. simplified.

    var json = [{ "id": "5001", "type": "None" },
                    { "id": "5002", "type": "Glazed" },
                    { "id": "5005", "type": "Sugar" },
                    { "id": "5003", "type": "Chocolate" },
                    { "id": "5004", "type": "Maple" },
                    { "id": "5009", "type": "Juice" }];           
    
            var JsonObject= JSON.parse(json);
                $.each(JsonObject, function(key,value) {
                    if( JsonObject[key].id=='5005' ){
                        JsonObject[key].id='1234';
                    }             
    
                    if( JsonObject[key].type=='Chocolate' ){
                        JsonObject[key].type='Only water';
                    }
    
                });
    
    0 讨论(0)
提交回复
热议问题