我有以下数据:
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
条目正在改变(不相关的方式)我正在尝试创建一个过滤结果的函数,并且只返回满足location
和company
条件的对象。
在上面的例子中,我们需要返回的是:
{
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"
}
答案 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.keys
和Array#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.filter
,Array.prototype.every
和Object.keys
来获得如下所需的结果:(请注意,obj
可以包含任意数量的密钥,这样就很灵活了)< / p>
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;
答案 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: [] }));
.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: [] }));