将对象数组的所有值与对象

时间:2017-02-10 16:56:06

标签: javascript arrays object lodash

我有以下数据:

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"]
}

并且假设myObj.company条目正在改变(不相关的方式)我正在尝试创建一个过滤结果的函数,并且只返回满足locationcompany条件的对象。

在上面的例子中,我们需要返回的是:

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

如果myObj

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

然后返回的结果应为

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

3 个答案:

答案 0 :(得分:2)

使用Array#filter方法和Array#includes方法(旧浏览器支持使用Array#indexOf方法)

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)))
)




更新:如果有多个属性集合,那么您需要进行一些变体,您可以使用Object.keysArray#every方法。

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])))
)




答案 1 :(得分:1)

使用Array.prototype.filterArray.prototype.everyObject.keys来获得如下所需的结果:(请注意,obj可以包含任意数量的密钥,这样就很灵活了)< / p>

&#13;
&#13;
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));
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以通过迭代条件的键进行过滤,并检查内容是否相等,如果数组的长度不为零。

 export psw='text'
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: [] }));

使用Array#includes代替Array#some

.as-console-wrapper { max-height: 100% !important; top: 0; }
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: [] }));