如何从Angular中的JSON数据中提取特定键?

时间:2017-02-23 08:43:32

标签: javascript html angularjs arrays json

我有以下JSON,我想提取唯一的位置,并将其放在另一个JSON或数组中,格式与以前相同。我是新手,因此,对我来说有点困惑。

这就是我所拥有的:

let data = {
    "Person"  : [{"name":"harman","comment_count":255},
                 {"name":"aman","comment_count":66},
                 {"name":"mukesh","comment_count":66}],
    "location": [{"name":"delhi","comment_count":255},
                 {"name":"mumbai","comment_count":66},
                 {"name":"chandigarh","comment_count":66}],
    "org     ": [{"name":"dell","comment_count":255},
                 {"name":"apple","comment_count":66},
                 {"name":"hp","comment_count":66}],
    "teams"  :  [{"name":"Real","comment_count":255},
                 {"name":"Juve","comment_count":66},
                 {"name":"Liverpool","comment_count":66}]        
};

这就是我想要的:

data2 = {
   "location": [
        {"name":"delhi","comment_count":255},
        {"name":"mumbai","comment_count":66},
        {"name":"chandigarh","comment_count":66}]       
};

2 个答案:

答案 0 :(得分:2)

包含下面的代码段。只需使用var data2 = data.location;祝你好运

  
  window.onload = function(){
  
      let data = {
                        "Person"  : [{"name":"harman","comment_count":255},
                                    {"name":"aman","comment_count":66},
                                    {"name":"mukesh","comment_count":66}],
                        "location": [{"name":"delhi","comment_count":255},
                                    {"name":"mumbai","comment_count":66},
                                    {"name":"chandigarh","comment_count":66}],
                        "org     ": [{"name":"dell","comment_count":255},
                                     {"name":"apple","comment_count":66},
                                     {"name":"hp","comment_count":66}],
                        "teams"  : [{"name":"Real","comment_count":255},
                                    {"name":"Juve","comment_count":66},
                                    {"name":"Liverpool","comment_count":66}]        
        };
        
       var data2 = data.location;
       console.log("data2:");
       console.log(data2);
  
  };
  

        
        

答案 1 :(得分:1)

这样做。

data2 = data.location