根据包含来自另一个数组的所有值的数组 属性 过滤对象数组
Filter an array of objects based on array property containing all values from another array
我有一个对象数组:
const breeds=[{name: 'Golden', temperament: ['friendly', 'kind', 'smart']},{name: 'Husky', temperament: ['alert', 'loyal', 'gentle']},{name: 'Yorkshire Terrier', temperament: ['bold', 'independent', 'kind']}]
我想按选择对它们进行排序 "temperament." 假设用户同时选择了 "kind" 和 "friendly",它应该只 return "Golden" .
我正在使用 javascript 和下划线,这是我迄今为止尝试过的方法:
//selected is an array of selected temperaments
//breeds is the array of objects
function filterTemperaments(selected, breeds) {
return _.filter(breeds, function (breed) {
if (!breed.temperament) breed.temperament = "";
const breedList = breed.temperament;
return breedList.includes(...selected);
}, selected);
}
这似乎只是 returning 符合所选数组中第一个气质的品种。例如,如果选择的是 ['kind', 'loyal']
并且品种是 {name:'Golden', temperament: ['kind', 'smelly']}
,尽管不符合 "Loyal" 气质
,Golden 仍然会返回 true
这里有更好的解决方案吗?提前致谢!!
可以使用filter
to return only those breeds which have every
气质选择
const breeds=[{name: 'Golden', temperament: ['friendly', 'kind', 'smart']},{name: 'Husky', temperament: ['alert', 'loyal', 'gentle']},{name: 'Yorkshire Terrier', temperament: ['bold', 'independent', 'kind']}],
selected = ['kind', 'friendly']
const filtered = breeds.filter(b => selected.every(s => b.temperament.includes(s)))
console.log(filtered)
使用 filter
和 every
可以很容易地使用它来了解您是否有任何匹配以及 return 匹配的值。
const breeds = [{
name: 'Golden',
temperament: ['friendly', 'kind', 'smart']
}, {
name: 'Husky',
temperament: ['alert', 'loyal', 'gentle']
}, {
name: 'Yorkshire Terrier',
temperament: ['bold', 'independent', 'kind']
}]
//selected is an array of selected temperaments
//breeds is the array of objects
function filterTemperaments(selected, breeds) {
return breeds.filter(({ temperament }) => { //here I'm desctructuring the object only to get the temperaments.
return selected.every(selection => temperament.indexOf(selection) !== -1)
})
}
const result = filterTemperaments(['kind', 'friendly'], breeds);
console.log(result)
我有一个对象数组:
const breeds=[{name: 'Golden', temperament: ['friendly', 'kind', 'smart']},{name: 'Husky', temperament: ['alert', 'loyal', 'gentle']},{name: 'Yorkshire Terrier', temperament: ['bold', 'independent', 'kind']}]
我想按选择对它们进行排序 "temperament." 假设用户同时选择了 "kind" 和 "friendly",它应该只 return "Golden" .
我正在使用 javascript 和下划线,这是我迄今为止尝试过的方法:
//selected is an array of selected temperaments
//breeds is the array of objects
function filterTemperaments(selected, breeds) {
return _.filter(breeds, function (breed) {
if (!breed.temperament) breed.temperament = "";
const breedList = breed.temperament;
return breedList.includes(...selected);
}, selected);
}
这似乎只是 returning 符合所选数组中第一个气质的品种。例如,如果选择的是 ['kind', 'loyal']
并且品种是 {name:'Golden', temperament: ['kind', 'smelly']}
,尽管不符合 "Loyal" 气质
这里有更好的解决方案吗?提前致谢!!
可以使用filter
to return only those breeds which have every
气质选择
const breeds=[{name: 'Golden', temperament: ['friendly', 'kind', 'smart']},{name: 'Husky', temperament: ['alert', 'loyal', 'gentle']},{name: 'Yorkshire Terrier', temperament: ['bold', 'independent', 'kind']}],
selected = ['kind', 'friendly']
const filtered = breeds.filter(b => selected.every(s => b.temperament.includes(s)))
console.log(filtered)
使用 filter
和 every
可以很容易地使用它来了解您是否有任何匹配以及 return 匹配的值。
const breeds = [{
name: 'Golden',
temperament: ['friendly', 'kind', 'smart']
}, {
name: 'Husky',
temperament: ['alert', 'loyal', 'gentle']
}, {
name: 'Yorkshire Terrier',
temperament: ['bold', 'independent', 'kind']
}]
//selected is an array of selected temperaments
//breeds is the array of objects
function filterTemperaments(selected, breeds) {
return breeds.filter(({ temperament }) => { //here I'm desctructuring the object only to get the temperaments.
return selected.every(selection => temperament.indexOf(selection) !== -1)
})
}
const result = filterTemperaments(['kind', 'friendly'], breeds);
console.log(result)