Jsonobject How To Change Value Depend On Key Value?
I've a json like this : [ { 'pages': 'foo1', 'hasil': '' }, { 'pages': 'foo2', 'hasil': '' }, { 'pages': 'foo3',
Solution 1:
As fas as I understand your problem statment, are you looking for something like this?
var data = [
{
"pages": "foo1",
"hasil": ""
},
{
"pages": "foo2",
"hasil": ""
},
{
"pages": "foo3",
"hasil": ""
},
{
"pages": "foo4",
"hasil": ""
},
{
"pages": "foo5",
"hasil": ""
},
{
"pages": "foo6",
"hasil": ""
}
];
for(let key inthis.data){
if(this.data[key].pages == 'foo1'){
this.data[key].hasil = 'hello';
}
}
console.log(data);
Solution 2:
You could use find
;
var array = [
{
"pages": "foo1",
"hasil": ""
},
{
"pages": "foo2",
"hasil": ""
},
{
"pages": "foo3",
"hasil": ""
},
{
"pages": "foo4",
"hasil": ""
},
{
"pages": "foo5",
"hasil": ""
},
{
"pages": "foo6",
"hasil": ""
}
];
var foundItem = array.find(function(element) {
return element.pages == "foo5";
});
foundItem.hasil = "123";
console.log(array);
Solution 3:
Iterate over the json objects and apply hasil according to your requirements:
var data = [
{
"pages": "foo1",
"hasil": ""
},
{
"pages": "foo2",
"hasil": ""
},
{
"pages": "foo3",
"hasil": ""
},
{
"pages": "foo4",
"hasil": ""
},
{
"pages": "foo5",
"hasil": ""
},
{
"pages": "foo6",
"hasil": ""
}
];
for(let obj of data){
let pages = obj['pages'];
if (pages.includes('foo')) {
obj['hasil'] = "h" + pages;
}
}
console.log(data);
Post a Comment for "Jsonobject How To Change Value Depend On Key Value?"