How to change json key: value

//my json data var jsndata = "{ "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" }, { "id": "5009", "type": "Juice" }" 

How would I change "type": "Chocolate" => "type": "only water"
or .. "id": "5005" => "id": "1234"

My list is very long .. Do I need to get or set any value?

Note. My list is dynamic and always sorted by id or type.

Will this jsndata.id['5003']='1234' ?

var getval = jsndata.id['5005'].type get val .. (Sugar value)?

+6
source share
4 answers
 <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 test */ console.log(json); replaceByValue('id','5001','5010') console.log(json); replaceByValue('type','Chocolate','only water') console.log(json); </script> 
+20
source

Take a look at Pinch . Here is a brief example of how Pinch can 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; }); 
+2
source

try it. 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'; } }); 
+1
source

changed the function from above to be able to change all the key values ​​and increase it by 1. And you can go to 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; 
+1
source

Source: https://habr.com/ru/post/1333653/


All Articles