来自节中的JSON对象的节信息

时间:2017-03-29 20:35:53

标签: javascript json

我觉得这很难解释,因为你可以在标题中看到,但让我们说这是我的对象的样子,

{  
   "success":true,
   "objects":[  
      {  
         "name":"Stick",
         "value":"wood",
         "size":"large"
      }...
    ]
}

现在我试图获取对象名称为Stick的所有数据,所以基本上如果它"" name"坚持它应该返回,名称,价值和大小。

2 个答案:

答案 0 :(得分:1)

考虑到你所制定的限制,我会采取以下措施。

yourObject = {  
   "success":true,
   "objects":[  
      {  
         "name":"Stick",
         "value":"wood",
         "size":"large"
      },
      {
         "name":"another",
         "value":"object",
         "size":"kindaBig"
      },
      {
         "name":"Rock",
         "value":"mineral",
         "size":"huge"  
      }  
    ]
};

arr = yourObject.objects.filter((innerObject) => innerObject.name === "Stick");

console.log(arr);

答案 1 :(得分:0)

感谢ES5,你可以使用map和filter作为:

var obj = {  
     "success":true,
     "objects":[  
        {  
           "name":"Stick",
           "value":"wood",
           "size":"large"
        }
     ]
}.objects
 .filter(function(object) {return object.name === "Stick"})
 .map(function(object) {return {value: object.value, size: object.size}})