简体   繁体   English

当至少一个对象的属性与测试的属性匹配时,如何遍历对象数组并返回 true/false object

[英]How can loop over an array of objects and return true/false when at least one of the object's properties matches the properties of a test object

i have an array that looks like this我有一个看起来像这样的数组

let arrayExample = 
[
 {
 "size":"6M",
 "color":"Blue"
 }
,
 {
 "size":"2M",
 "color":"Yellow"
 }
]

currently, to return all elements of a specific color or a specific size I use this approach目前,要返回特定颜色或特定大小的所有元素,我使用这种方法

var allColors= arrayExample.map(i=>i.color)

return [{"color":"Blue"}].some((val)=>  allColors.includes(val.color))

my question is how to return a truthy value whenever i found an element with the exact combination i desire我的问题是,每当我找到一个具有我想要的精确组合的元素时,如何返回一个真值

as quoted in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/somehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Return value true if the callback function returns a truthy value for at least one element in the array.如果回调 function 为数组中的至少一个元素返回真值,则返回值 true。 Otherwise, false.否则,假的。

my current approach to this is我目前的做法是

var allColors= arrayExample.map(i=>i.color)
var allSizes= arrayExample.map(i=>i.size)

return [{"color":"Blue","size":"2M"}].some((val)=>  allColors.includes(val.color) && allSizes.includes(val.size)

this approach doesn't work for my purposes, knowing that even if there is no item in my arrayExample with the combination [{"color":"Blue","size":"2M"}] , it would still return true because of the 2M size of the 2nd item of my array这种方法对我的目的不起作用,知道即使我的arrayExample中没有项目与组合[{"color":"Blue","size":"2M"}] ,它仍然会返回true因为我数组第二项的 2M 大小

here is a Jsfiddle to test my situation https://jsfiddle.net/8htjpyb9/1/这是一个 Jsfiddle 来测试我的情况https://jsfiddle.net/8htjpyb9/1/

I would like to thank you if you got this far and also thank you for any and all help, I hope you are having a good day如果你走到这一步,我要感谢你,也感谢你提供的所有帮助,希望你今天过得愉快

*edit -> i would like to emphasize that.filter does not work for my problem because of the rest of the code this situation is embedded in. *编辑 -> 我想强调的是,由于嵌入了这种情况的代码的 rest,过滤器不适用于我的问题。

I am only interested in returning a value of true whenever it finds the exact match.我只对在找到完全匹配时返回 true 值感兴趣。

  • Using Array#map , iterate over the array使用Array#map遍历数组
  • In every iteration, using Array#some , check if any of the combinations match the current object.在每次迭代中,使用Array#some检查是否有任何combinations与当前 object 匹配。
  • To do so, you can use Object#entries and Array#every to check a combination of properties matching the current object.为此,您可以使用Object#entriesArray#every来检查与当前 object 匹配的属性组合。

 const array = [ { id: 1, size: '6M', color: 'Blue' }, { ld: 2, size: '2M', color: 'Yellow' }, { id: 3, size: '6M', color: 'Blue' } ]; const combinations = [ { "color": "Blue", "size": "6M" } ]; const matches = array.map(current => combinations.some(combination => Object.entries(combination).every(([key, value]) => current[key] === value ) ) ); console.log(matches);

If I understand correctly you are looking for this?如果我没理解错你在找这个?

 const getItem = (arr, toFind) => { const {size, color} = toFind; return arr.filter(item => item.color === color && item.size === size).length > 0 }; let arrayExample = [{ "size": "6M", "color": "Blue", "material": "plastic" }, { "size": "2M", "color": "Yellow", "material": "leather" } ] const res = getItem(arrayExample,{"size": "6M","color": "Blue"}) console.log(res)

If you want to return the objects the match filter would be an easier approach.如果您想返回对象,匹配filter将是一种更简单的方法。 Pass in a set of data, and a test object to a function and return only those object that have the same size and colour values as the test object.传入一组数据,将测试 object 传递给 function,并仅返回与测试 object 具有相同大小和颜色值的那些 object。

 const data=[{id: 1,size:'6M',color:'Blue'},{ld:2,size:'2M',color:'Yellow'},{id:3,size:'6M',color:'Blue'}]; const test = { size: '6M', color: 'Blue' }; function getExact(data, test) { return data.filter(obj => { return ( obj.size === test.size && obj.color === test.color ); }); } console.log(getExact(data, test));

If you only want to return true/false if the properties of an array object matches the properties of a test object some is the better option.如果您只想在数组 object 的属性与测试 object 的属性匹配时返回true/false ,那么some更好的选择。

 const data=[{id: 1,size:'6M',color:'Blue'},{ld:2,size:'2M',color:'Yellow'},{id:3,size:'6M',color:'Blue'}]; const test = { size: '6M', color: 'Blue' }; const test2 = { size: '5M', color: 'Green' }; function getExact(data, test) { return data.some(obj => { return ( obj.size === test.size && obj.color === test.color ); }); } console.log(getExact(data, test)); console.log(getExact(data, test2));

暂无
暂无

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

相关问题 如何克隆对象并迭代其中一个属性? - How can I clone an object and iterate over one of it's properties? 如何在与另一个数组中的对象匹配的数组中使用 object 属性 - How to use object properties in an array that matches objects in another array 当迭代对象数组中的对象属性时,foreach 循环不起作用 [javascript] - foreach loop not working when iterating over object properties from an array of objects [javascript] 每当 React 中的某个属性设置为 true 时,我如何将 object 属性切换为 false? - How would I toggle object properties to false, whenever one of the properties is set to true in React? 通过按字符串搜索键来查找对象属性,如果为真,则返回为真 - Find an object properties by searching for keys by a string, and if one is true, return as true 如果数组对象的 2 个属性相等,我如何删除数组中的对象? - How can i remove the object in the array if 2 properties of array objects are equal? 如何将一组对象缩减为一个具有唯一属性的 object? JavaScript - How to reduce an array of objects into one object with unique properties? JavaScript 如何根据另一个数组对象的某些属性创建一个对象数组? - How can I create an array of objects based on some of another array object's properties? 如何在 Angular 中使用 ngFor 遍历对象属性 - How to loop over object properties with ngFor in Angular 如何从其属性之一返回数组中的对象? - How to return an object in an array from one of its properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM