Reputation: 1243
New to manipulating JSON, I appreciate the help! This project uses VueJs 2 if that makes a difference.
I am trying to update a key value, in this example it is "group" for a specific applicant identified by the ID.
I am trying to accomplish something along of lines of:
WHERE applicantID = 3 SET group = 4
This is a sample of the JSON I am dealing with:
{
"applicantID" : 3,
"fullName": "name",
"value1": 30,
"value1": 31,
"value1": 40,
"value1": 41,
"value1": "50",
"value1": "51",
"group": 0,
"flag": true,
},
{
"applicantID" : 4,
"fullName": "name",
"value1": 30,
"value1": 31,
"value1": 40,
"value1": 41,
"value1": "50",
"value1": "51",
"group": 0,
"flag": false,
}
Upvotes: 1
Views: 18093
Reputation: 75
You can update a specific value in a JSON array by first getting the specific index then accessing the object property name to edit the value as illustrated below
let users = [{name:"John", DOB:"1991", gender:"male"}, {name:"mary", DOB:"1993", gender:"female"}];
//change DOB of Mary
users[1].DOB = "1995";
NB You can also loop over the JSON array to update a value at a specific index using the same logic.
Upvotes: 0
Reputation: 778
if you have to compare multiple fields you can use this .as an adjustment onto Sergii's answer
var item = array.find(x => {
return x.applicantID == 3 && x.fullName == "name" ;
});
if (item) {
item.group = 4;
}
Upvotes: 1
Reputation: 2684
You can do it like this:
var item = array.find(x => x.applicantID == 3);
if (item) {
item.group = 4;
}
It will change a value of the group in the original array.
Upvotes: 8