简体   繁体   中英

Compare all values of an Array of Objects with an Object

I have the following data:

 const myArr = [{ id: 0, company: "microsoft", location: "berlin" }, { id: 1, company: "google", location: "london" }, { id: 2, company: "twitter", location: "berlin" }]; let myObj = { company: ["google", "twitter"], location: ["london"] } 

and given that the myObj.company entries are changing (irrelevant how) I am trying to create a function that filters results and only returns Objects that satisfy the location and company criteria.

In the example above, what we need returned is :

{ 
  id: 1,
  company: "google",
  location: "london"
}

If myObj was

let myObj = {
  company: ["google", "twitter"],
  location: []
}

then the returned result should be

{ 
  id: 1,
  company: "google",
  location: "london"
},
{ 
  id: 2,
  company: "twitter",
  location: "berlin"
}

Use Array#filter method with Array#includes method(for old browser support use Array#indexOf method)

myArr.filter(o => (myObj.company.length == 0 || myObj.company.includes(o.company)) && (myObj.location.length == 0 || myObj.location.includes(o.location)))

 const myArr = [{ id: 0, company: "microsoft", location: "berlin" }, { id: 1, company: "google", location: "london" }, { id: 2, company: "twitter", location: "berlin" }]; let myObj = { company: ["google", "twitter"], location: ["london"] } console.log( myArr.filter(o => (myObj.company.length == 0 || myObj.company.includes(o.company)) && (myObj.location.length == 0 || myObj.location.includes(o.location))) ) myObj = { company: ["google", "twitter"], location: [] } console.log( myArr.filter(o => (myObj.company.length == 0 || myObj.company.includes(o.company)) && (myObj.location.length == 0 || myObj.location.includes(o.location))) ) 


UPDATE : In case there an n number of properties collection then you need to make some variation, where you can use Object.keys and Array#every methods.

var keys = Object.keys(myObj);

myArr.filter(o => keys.every(k => myObj[k].length == 0 || myObj[k].includes(o[k])))

 const myArr = [{ id: 0, company: "microsoft", location: "berlin" }, { id: 1, company: "google", location: "london" }, { id: 2, company: "twitter", location: "berlin" }]; let myObj = { company: ["google", "twitter"], location: ["london"] } var keys = Object.keys(myObj); console.log( myArr.filter(o => keys.every(k => myObj[k].length == 0 || myObj[k].includes(o[k]))) ) myObj = { company: ["google", "twitter"], location: [] } keys = Object.keys(myObj); console.log( myArr.filter(o => keys.every(k => myObj[k].length == 0 || myObj[k].includes(o[k]))) ) 

Use Array.prototype.filter , Array.prototype.every and Object.keys to get the desired result like this: (note that obj could have any number of keys, it flexible that way)

 const myArr = [{ id: 0, company: "microsoft", location: "berlin" }, { id: 1, company: "google", location: "london" }, { id: 2, company: "twitter", location: "berlin" }]; let myObj = { company: ["google", "twitter"], location: ["london"] } function find(arr, obj) { // get only the keys from obj that their corresponding array is not empty var keys = Object.keys(obj).filter(k => obj[k].length !== 0); // return a filtered array of objects that ... return arr.filter(o => { // ... met the creteria (for every key k in obj, the current object o must have its value of the key k includded in the array obj[k]) return keys.every(k => { return obj[k].indexOf(o[k]) != -1; }); }); } console.log(find(myArr, myObj)); 

You could filter with iterating the keys of criteria and check the content for equality of if the length of the array is not zero.

 const filterBy = (array, criteria) => array.filter(o => Object.keys(criteria).every(k => criteria[k].some(c => c === o[k]) || !criteria[k].length) ); const myArr = [{ id: 0, company: "microsoft", location: "berlin" }, { id: 1, company: "google", location: "london" }, { id: 2, company: "twitter", location: "berlin" }]; console.log(filterBy(myArr, { company: ["google", "twitter"], location: ["london"] })); console.log(filterBy(myArr, { company: ["google", "twitter"], location: [] })); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

With Array#includes instead of Array#some

 const filterBy = (array, criteria) => array.filter(o => Object.keys(criteria).every(k => criteria[k].includes(o[k]) || !criteria[k].length) ); const myArr = [{ id: 0, company: "microsoft", location: "berlin" }, { id: 1, company: "google", location: "london" }, { id: 2, company: "twitter", location: "berlin" }]; console.log(filterBy(myArr, { company: ["google", "twitter"], location: ["london"] })); console.log(filterBy(myArr, { company: ["google", "twitter"], location: [] })); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM