0

I have some objects in an array:

var names = [
    { id:"101", name:"eric" },
    { id:"202", name:"john" },
    { id:"303", name:"zack" },
    { id:"404", name:"ron" }
];

I want to remove one of the objects. I have only one piece data to check the object array against, which is the id = 202. Finally, I want to return the adjusted array, like this:

[
    { id:"101", name:"eric" },
    { id:"303", name:"zack" },
    { id:"404", name:"ron" }
];
2

3 Answers 3

4

In javascript you can use either Array.filter or Array.reject to return an array of objects that you want. Below I have demonstrated how to achieve this using filter.

names.filter(function(eachObject){ return eachObject.id !== '202';})

Array.filter is a native function that will take an existing array and return a new array with the filtered values. It does this by iterating over each item in the array and running a predicate/truth function on every single item. If an item passes the truth test it is pushed into the new array. Once the iterating is complete, filter will return a brand new array with only the values that pass the test.

In the example above the predicate function is an anonymous function that we passed into filter. You can also pass in a function that has been previously declared like so.

function idNotTwoZeroTwo(eachObject){ return eachObject.id !== '202'};
names.filter(idNotTwoZeroTwo) // gives you same result

Also, you are not actually representing proper JSON in your example. If you have time go through a quick tutorial at http://www.w3schools.com/json/default.asp to brush up.

One more thing. If in the future you want to check to see if something is in proper JSON just go to http://jsonlint.com/ and copy your code in. It will tell you wether or not your code is correct JSON. It will even tell you exactly what is wrong.

This is my first answer so Im sorry if I didn't answer your question fully. Hope it helps.!

1

With the use of filter:

var res = names.filter(function(obj) {
    return obj.id !== "202";
});

console.log(res);
0
0

Hi u can use underscore js very simple

var names = [
    { id:"101", name:"eric" },
    { id:"202", name:"john" },
    { id:"303", name:"zack" },
    { id:"404", name:"ron" }
];

console.log(_.reject(names, function(num){ return num.id == "202"; }))

include the library underscore

3
  • 1
    There is no need to use a library!
    – user3589620
    Jun 10, 2016 at 2:02
  • Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects, Underscore provides over 100 functions. in future if he wants to write many operations better to use this , its my suggestion Jun 10, 2016 at 2:04
  • 1
    It is better to learn and understand plain JavaScript first. Then, if it is usefull, one can look into a helpfull library for special cases.
    – user3589620
    Jun 10, 2016 at 2:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.