Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter array if element is present in another array based on value

I have an object array like this:

var objectArray = [{id_5:"100"},
                   {id_1:"300"},
                   {id_2:"500"},
                   {id_4:"700"},
                   {id_3:"200"}];

And a normal array like this:

var normalArray = ["id_2","id_5","id_4"];

I want to subtract every element from the objectArray if there's a matching ID in the normalArray. I then want to order the newly created array by the object's value (lowest value being first).

So for the above example the result would be:

var newObjectArray = [{id_3:"200"},
                      {id_1:"300"}];

Is it possible to do this without jQuery?

I've seen similar questions like this one: Removing object from one array if present in another array based on value but I haven't been able to find an answer that works. Is it possible to do a compare and remove like this while still keeping the key:value pairs intact? Thanks in advance for any help with this!

like image 885
Emily Avatar asked Feb 17 '17 11:02

Emily


People also ask

How do you filter an array of objects based on another array?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How do you check if an element of an array is in another array?

Use the inbuilt ES6 function some() to iterate through each and every element of first array and to test the array. Use the inbuilt function includes() with second array to check if element exist in the first array or not. If element exist then return true else return false.

How can we filter an array of elements from a given array in ES6?

ES6 | Array filter() Method Callback: The function is a predicate, to test each element of the array. Return true to keep the element, false otherwise. It accepts three arguments: element: The current element being processed in the array.


2 Answers

You should use filter method, which accepts as parameter a callback function.

Also, you have to use Object.keys() method in order to get the key of each object in objectArray.

To check if an element from objectArray doesn't appear in objectArray, you should use indexOf() method.

To achieve the sorted array, you have to use sort() method.

var objectArray = [{id_5:"100"},
                   {id_1:"300"},
                   {id_2:"500"},
                   {id_4:"700"},
                   {id_3:"200"}];

var normalArray = ["id_2","id_5","id_4"];
var newArray=objectArray.filter(function(item){
    return normalArray.indexOf(Object.keys(item)[0]) == -1;
}).sort(function(a,b){
    return a[Object.keys(a)[0]] - b[Object.keys(b)[0]];
});
console.log(newArray);
like image 187
Mihai Alexandru-Ionut Avatar answered Sep 30 '22 01:09

Mihai Alexandru-Ionut


You can first use filter() to remove object with key from other array and then sort to sort result array by objects value.

var objectArray = [{id_5:"100"},{id_1:"300"},{id_2:"500"},{id_4:"700"},{id_3:"200"}];
var normalArray = ["id_2","id_5","id_4"];

var newArray = objectArray
  .filter(e => !normalArray.includes(Object.keys(e)[0]))
  .sort((a, b) => a[Object.keys(a)[0]] - b[Object.keys(b)[0]])
  
console.log(newArray)
like image 34
Nenad Vracar Avatar answered Sep 30 '22 01:09

Nenad Vracar