简体   繁体   中英

Javascript : add/remove json element in json object

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.

Other 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.

eg

delete candidate["skills"];

candidate["skills"] = "javascript";

For removing:

candidate.delete("skills");

For adding:

candidate.skills = "javscript"

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.

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM