【问题标题】:How to extract a specific key from the JSON data in Angular?如何从 Angular 中的 JSON 数据中提取特定密钥?
【发布时间】:2017-07-13 15:23:16
【问题描述】:

我有以下 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}]       
};

【问题讨论】:

  • data2.location = data.location;

标签: javascript html angularjs arrays json


【解决方案1】:

在下面包含一个 sn-p。只需使用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);
  
  };
  

        
        

【讨论】:

    【解决方案2】:

    这样做。

    data2 = data.location
    

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 2021-02-05
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2013-05-29
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多