gpt4 book ai didi

JavaScript,按特定键的值过滤对象数组

转载 作者:行者123 更新时间:2023-12-02 18:09:55 25 4
gpt4 key购买 nike

我正在尝试找到使用特定键的字符串过滤对象数组的最佳方法。基本上我想要实现的是获取包含 "Type":"Blue" 的对象。这是我的数据:

[
{
"data": [
{}
],
"Name": "1",
"Type": "Blue"
},
{
"data": [
{}
],
"Name": "2",
"Type": "Red"
},
{
"data": [
{}
],
"Name": "3",
"Type": "Blue"
}
]

最佳答案

您可以使用filter()方法。请参阅下面的代码片段以及 MDN 中该方法的定义:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

const data = [
{
data: [{}],
Name: "1",
Type: "Blue"
},
{
data: [{}],
Name: "2",
Type: "Red"
},
{
data: [{}],
Name: "3",
Type: "Blue"
}
];

const filteredData = data.filter((item) => item.Type === "Blue");
console.log(filteredData);

关于JavaScript,按特定键的值过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72558058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com