-1

In a given Javascript object I want to remove item with id#4. How can I do that?

var myObj =[ {id: 1, name: "Maria"}, {id: 2, name: "Josef"}, {id: 3, name: "Jesus"}, {id: 4, name: "Mohammad"} ] 

something like delete myObj.id = 4??

thx in advance...

6
  • 2
    You do realize that javascript object is completely invalid ? Commented Nov 8, 2014 at 21:19
  • I think your example has error. Can you change it ? Commented Nov 8, 2014 at 21:19
  • You (probably) mean: [{id : 1, name : 'Maria'}], right? An object of objects would require each object to be mapped to a specific key, an array of objects would be easier. Commented Nov 8, 2014 at 21:19
  • Have you looked at this question, yet: stackoverflow.com/questions/15287865/… Commented Nov 8, 2014 at 21:24
  • Thank you David Thomas - I think your link is what im looking for Commented Nov 8, 2014 at 21:26

1 Answer 1

0

Let's go back to your original question of deleting an object from your object.

Let's say you created your object as:

var myObj = {}; var key = 1; myObj[key] = {id: key, name: "Maria"}; key = 2; myObj[key] = {id: key, name: "Josef"} key = 3; myObj[key] = {id: key, name: "Jesus"} key = 4; myObj[key] = {id: key, name: "Mohammad"} 

which would look like:

var myObj ={ 1: {id: 1, name: "Maria"}, 2: {id: 2, name: "Josef"}, 3: {id: 3, name: "Jesus"}, 4: {id: 4, name: "Mohammad"} }; 

Then you could delete an object from your object using it's key

key = 4; delete myObj[key]; 

In this case you can key off of any value you want and not have to worry about filtering.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.