简体   繁体   English

根据包含来自另一个数组的所有值的数组属性过滤对象数组

[英]Filter an array of objects based on array property containing all values from another array

I have an array of objects: 我有一个对象数组:

const breeds=[{name: 'Golden', temperament: ['friendly', 'kind', 'smart']},{name: 'Husky', temperament: ['alert', 'loyal', 'gentle']},{name: 'Yorkshire Terrier', temperament: ['bold', 'independent', 'kind']}]

I would like to sort them by selected "temperament." 我想按选定的“气质”对它们进行排序。 Let's say a user has selected both "kind" & "friendly", it should only return "Golden". 假设用户同时选择了“种类”和“友好”,则应该只返回“金色”。

I'm using javascript and underscore and this is what I have tried so far: 我正在使用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);
}

This seems to only be returning breeds that match the first temperament in the selected array. 这似乎只是返回与所选阵列中第一个性格相符的品种。 For example if selected is ['kind', 'loyal'] and the breed is {name:'Golden', temperament: ['kind', 'smelly']} , Golden will still come back as true, despite not matching the "Loyal" temperament 例如,如果选择的是['kind', 'loyal'] ,品种是{name:'Golden', temperament: ['kind', 'smelly']} ,尽管与“忠诚”的气质

Any thoughts for a better solution here? 有什么更好的解决方案的想法吗? Thanks in advance!! 提前致谢!!

You can use filter to return only those breeds which have every temperament selected 您可以使用filter仅返回选择了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) 

it is easy to use using filter and every in order to know if you got any matches and return the matched values. 使用filterevery易于使用,以便知道是否有任何匹配项并返回匹配的值。

 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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据来自另一个对象数组的多个值过滤对象数组 - Filter array of objects based on multiple values from another array of objects JavaScript根据另一个对象数组的属性过滤对象数组 - JavaScript Filter array of objects based on property of another array of objects 对象数组,某些对象包含另一个数组,需要通过包含数组的对象中的值对其进行过滤 - Array of objects with some objects containing another array, need to filter it by values within the object containing an array 如何根据公共属性的值从另一个对象数组的所有元素中过滤一个对象数组 - How to filter an arrayof objects from all elements of another array of object on values of a common property 根据来自不同对象数组的属性和值过滤对象数组 - Filter an array of objects based on a property and value from a different array of objects 根据另一个包含 angular js 对象的数组过滤包含对象的数组 - Filter an array containing objects based on another array containing objects in angular js 根据另一个对象数组从对象数组中过滤数据 - filter data from an array of objects based on another array of objects 通过AngularJS中包含子字符串的属性过滤对象数组 - Filter an array of objects by a property containing a substring in AngularJS 按另一个对象数组的属性按整数过滤数组 - filter array by integers by property of another array of objects 如何使用基于另一个数组的值过滤对象数组 - How to filter array of objects with based on value from another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM