//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" }"
Run Code Online (Sandbox Code Playgroud)
如何改变"类型":"巧克力"值=>"类型":"仅水"
或""id":"5005",=>"id":"1234",
我的列表很长..我需要获取任何值或设置任何值?
注意:我的清单是动态的.总是按ID或类型排序顺序..
jsndata.id ['5003'] ='1234'改变..?var getval = jsndata.id ['5005'].type get val ..(value Sugar)?
:d
St.*_*and 21
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68726 次 |
| 最近记录: |