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.

2
  • In JS , You can directly access candidate variable and assign only javascript to it . candidate.skills = "javascript" 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 Commented Jun 24, 2016 at 10:02

8 Answers 8

10

Other ways it can be achieved.

For Adding:

candidate["skills"] = "javascript";

For Deleting:

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

or

delete candidate.skills;
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

For removing:

How do I remove a property from a JavaScript object?

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

Comments

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";

Comments

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.

Comments

0

For removing:

candidate.delete("skills");

For adding:

candidate.skills = "javscript"

2 Comments

delete candidate.skills;
Your code has syntax error. @RolandStarke proposed the correction.
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);

1 Comment

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?. Thanks
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];
}

1 Comment

delete candidate.skills; will remove prop with value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.