I need to modify my JSON Object by add/remove Json element. Here is my JSON Object,
var candidate = {
"name":"lokesh",
"age":26,
"skills":[
"Java",
"Node Js",
"Javascript"
]
};
I need to remove the element "skills" and output should be like,
{
"name":"lokesh",
"age":26
}
And I again I need to add the element "skills" as string and the output should be like,
{
"name":"lokesh",
"age":26,
"skills":"javascript"
}
Please help,
Thanks in advance.
I need to modify my JSON Object by add/remove Json element. Here is my JSON Object,
var candidate = {
"name":"lokesh",
"age":26,
"skills":[
"Java",
"Node Js",
"Javascript"
]
};
I need to remove the element "skills" and output should be like,
{
"name":"lokesh",
"age":26
}
And I again I need to add the element "skills" as string and the output should be like,
{
"name":"lokesh",
"age":26,
"skills":"javascript"
}
Please help,
Thanks in advance.
Share Improve this question asked Jun 24, 2016 at 9:53 lokeshkumarlokeshkumar 511 gold badge1 silver badge4 bronze badges 2- In JS , You can directly access candidate variable and assign only javascript to it . candidate.skills = "javascript" – sathya Commented Jun 24, 2016 at 9:57
- Add the code you have written so far and which problem are you facing. You must demonstrate some research was done before asking. See mvce – flaviodesousa Commented Jun 24, 2016 at 10:02
8 Answers
Reset to default 10Other ways it can be achieved.
For Adding:
candidate["skills"] = "javascript";
For Deleting:
var skill = "javascript";
delete candidate[skill];
or
delete candidate.skills;
Removing a property of an object can be done by using the delete keyword:
candidate.delete("skills");
OR
delete candidate["skills"];
To add a property to an existing object in JS you could do the following.
candidate["skills"] = "javscript";
OR
candidate.skills = "javscript";
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/delete
For removing:
How do I remove a property from a JavaScript object?
For adding, just set the new value for that property.
You can directly assign "javscript" to "skills" key.
var candidate = {
"name":"lokesh",
"age":26,
"skills":[
"Java",
"Node Js",
"Javascript"
]
};
You can directly do this.
candidate.skills = "javascript";
Or you can delete the "skills" key and add it again.
e.g.
delete candidate["skills"];
candidate["skills"] = "javascript";
You should use delete method to delete members of an object.
delete candidate.skills;
Note: You cannot delete objects or functions in the global scope with delete keyword. However you can delete a function which is a member of an object.
For removing:
candidate.delete("skills");
For adding:
candidate.skills = "javscript"
This piece of coude should do it:
var candidate = {
"name":"lokesh",
"age":26,
"skills":[
"Java",
"Node Js",
"Javascript"
]
};
delete candidate.skills;
console.log(candidate);
candidate["skills"] = "javascript";
console.log(candidate);
this is your json object
var candidate = {
"name":"lokesh",
"age":26,
"skills":[
"Java",
"Node Js",
"Javascript"
]
};
to read any prop you can call like:
var name = candidate.name; or candidate[0].name
var age = candidate.age; or candidate[0].age;
var skills = candidate.skills; or candidate[0].skills;
to write any of prop: candidate.name = 'moaz'; candidate.age = 35;
remove skills value:
candidate.skills = null; or candidate.skills = '';
here skills is list of json if I use as string value or list of string:
candidate.skills = 'javasccript'; //Use skills as string value
candidate.skills.push('javascript'); //Use skills as list of string
to read skills:
for (var i = 0; i < candidate.skills.length; i++) {
var skill = candidate.skills[i];
}