简体   繁体   English

遍历数组并获取具有特定字符串的对象

[英]Loop through array and get objects with a certain string

ex: 例如:

{
  "data": [
    {
      "name": "grape",
      "color": "purple"
    },
    {
      "name": "apple",
      "color": "green"
    }
  ]
}

Let's say I ONLY want to get objects with the color of purple. 比方说,我只是想获得与物体color的紫色。 How would I do this? 我该怎么做?

Something like this? 像这样吗 You can use JavaScript's Array.filter() . 您可以使用JavaScript的Array.filter()

 const obj = { "data": [ { "name": "grape", "color": "purple" }, { "name": "apple", "color": "green" } ] }; const result = obj.data.filter(element => element.color === 'purple'); console.log(result); 

This filters through the array and returns a list of objects with the color of purple. 该过滤器通过数组,并返回与对象的列表color的紫色。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM