JSONObject如何根据键值更改值? [英] JSONObject how to change value depend on key value?
问题描述
我有一个像这样的json:
I've a json like this :
[
{
"pages": "foo1",
"hasil": ""
},
{
"pages": "foo2",
"hasil": ""
},
{
"pages": "foo3",
"hasil": ""
},
{
"pages": "foo4",
"hasil": ""
},
{
"pages": "foo5",
"hasil": ""
},
{
"pages": "foo6",
"hasil": ""
}
]
所以我想根据pages
值更改hasil
键的值,
我该怎么办?
so I want to change value of hasil
key depending on pages
value,
how can I do that?
编辑以明确询问
我想像这样更改特定json键的值:
I want to change value of specific json key like this :
[
{
"pages": "foo1",
"hasil": ""
},
{
"pages": "foo2",
"hasil": ""
},
{
"pages": "foo3",
"hasil": ""
},
{
"pages": "foo4",
"hasil": ""
},
{
"pages": "foo5",
"hasil": ""
},
{
"pages": "foo6",
"hasil": "1"
}
]
研究: w3school ,推荐答案
据我了解您的问题陈述,您是否正在寻找类似的东西? As fas as I understand your problem statment, are you looking for something like this?
这篇关于JSONObject如何根据键值更改值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!var data = [
{
"pages": "foo1",
"hasil": ""
},
{
"pages": "foo2",
"hasil": ""
},
{
"pages": "foo3",
"hasil": ""
},
{
"pages": "foo4",
"hasil": ""
},
{
"pages": "foo5",
"hasil": ""
},
{
"pages": "foo6",
"hasil": ""
}
];
for(let key in this.data){
if(this.data[key].pages == 'foo1'){
this.data[key].hasil = 'hello';
}
}
console.log(data);