根据匹配值从另一个数组获取新数组
Getting a new array from another array based on matching value
我有一个包含数千个对象的数组,需要检查其中一个值中是否包含特定值。
我的数组:
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
},
...
]
我正在尝试获取一个新数组,其中仅包含那些包含特定颜色的对象。此外,该值必须是完美匹配:即如果我通过“blue”,如果它检查“dark blue”
,它应该 return false
我正在尝试执行以下操作,但 运行 出现错误
const checkColourtHandler = async (selectedColour) => {
const imageList = images.filter(function (item, i) {
if (item.colours.includes(selectedColour)) {
return true;
} else return false;
});
console.log(imageList);
}
理想情况下,我也在寻找性能最好的方法。
应该这样做
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
}
];
const checkColourtHandler = (selectedColour) => images.filter(item => item.colours.includes(selectedColour));
console.log(checkColourtHandler("sand"));
console.log(checkColourtHandler("violet"));
使用 filter
和 includes
方法检查匹配元素:
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
}
];
const filtered = (col) => images.filter(i => i.colours.includes(col));
const output = filtered('dark blue');
console.log(output);
console.log(filtered('blue'));
@DigitalDrifter
在这里更新我的代码
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
}
];
const filtered = (col) => images.filter(i => i.colours.includes(col));
const checkColourtHandler = (selectedColour) => {
const result = filtered(selectedColour);
console.log("result: ",result);
}
checkColourtHandler('pink');
我有一个包含数千个对象的数组,需要检查其中一个值中是否包含特定值。
我的数组:
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
},
...
]
我正在尝试获取一个新数组,其中仅包含那些包含特定颜色的对象。此外,该值必须是完美匹配:即如果我通过“blue”,如果它检查“dark blue”
,它应该 return false我正在尝试执行以下操作,但 运行 出现错误
const checkColourtHandler = async (selectedColour) => {
const imageList = images.filter(function (item, i) {
if (item.colours.includes(selectedColour)) {
return true;
} else return false;
});
console.log(imageList);
}
理想情况下,我也在寻找性能最好的方法。
应该这样做
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
}
];
const checkColourtHandler = (selectedColour) => images.filter(item => item.colours.includes(selectedColour));
console.log(checkColourtHandler("sand"));
console.log(checkColourtHandler("violet"));
使用 filter
和 includes
方法检查匹配元素:
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
}
];
const filtered = (col) => images.filter(i => i.colours.includes(col));
const output = filtered('dark blue');
console.log(output);
console.log(filtered('blue'));
@DigitalDrifter
在这里更新我的代码
const images = [
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["pink", "yellow", "red"],
},
{
"name" : "old car",
"width" : "90",
"height" : "150",
"colours" : ["dark purple", "sand", "light green"],
},
{
"name" : "sky",
"width" : "90",
"height" : "150",
"colours" : ["dark blue", "violet"],
}
];
const filtered = (col) => images.filter(i => i.colours.includes(col));
const checkColourtHandler = (selectedColour) => {
const result = filtered(selectedColour);
console.log("result: ",result);
}
checkColourtHandler('pink');