5

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.

lokeshkumar
  • 51
  • 1
  • 1
  • 4
  • In JS , You can directly access candidate variable and assign only javascript to it . candidate.skills = "javascript" – sathya Jun 24 '16 at 09: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](http://stackoverflow.com/help/mcve) – flaviodesousa Jun 24 '16 at 10:02

8 Answers8

10

Other ways it can be achieved.

For Adding:

candidate["skills"] = "javascript";

For Deleting:

var skill = "javascript";
delete candidate[skill];

or

delete candidate.skills;
Praveen
  • 657
  • 3
  • 9
  • 23
1

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

Ninju
  • 2,522
  • 2
  • 15
  • 21
1

For removing:

How do I remove a property from a JavaScript object?

For adding, just set the new value for that property.

mauretto
  • 3,183
  • 3
  • 27
  • 28
1

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";
Pang
  • 9,564
  • 146
  • 81
  • 122
Abhijit
  • 242
  • 1
  • 7
1

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.

Hith
  • 308
  • 1
  • 6
  • 16
0

For removing:

candidate.delete("skills");

For adding:

candidate.skills = "javscript"
Subburaj
  • 5,114
  • 10
  • 44
  • 87
0

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);
Pablo Abad
  • 62
  • 3
  • Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Sep 21 '18 at 08:07
0

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];
}
Moaz Salem
  • 438
  • 3
  • 5