[英]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/some如https://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 值感兴趣。
Array#map
, iterate over the array使用Array#map
遍历数组Array#some
, check if any of the combinations
match the current object.在每次迭代中,使用Array#some
检查是否有任何combinations
与当前 object 匹配。Object#entries
and Array#every
to check a combination of properties matching the current object.为此,您可以使用Object#entries
和Array#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.