2017-02-23 73 views
-5

我有以下的JSON,我想提取唯一的位置,並把它放在另一個JSON或數組中與以前相同的格式。我是一個新手,因此,對我來說有點困惑。如何從Angular的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}]  
}; 
+0

非常基本的東西 –

+1

'data2.location = data.location;' –

回答

1

做到這一點。

data2 = data.location 
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); 
 
    
 
    };