2017-04-03 63 views
-1

我有一个JSON数组如下:转换JSON阵列来选择键的JSON阵列

[{ 
"id": 1, 
"name": "Leanne Graham", 
"username": "Bret", 
"email": "[email protected]", 
"address": { 
    "street":"201 S 4th St.", 
    "suite": "Apt. 556", 
    "city": "Gwenborough", 
    "zipcode": "92998-3874", 
    "geo": { 
    "lat": "-37.3159", 
    "lng": "81.1496" 
    } 
}, 
"company": { 
    "name": "Romaguera-Crona", 
    "catchPhrase": "Multi-layered client-server neural-net", 
    "bs": "harness real-time e-markets" 
}, 
"hobbies":[ 
     { 
      "books":"fiction", 
      "sports":"football", 
      "music":"rock" 
     }, 

     { 
      "books":"action", 
      "sports":"cricket", 
      "music":"jazz" 
     }, 
     { 
      "books":"action", 
      "sports":"cricket", 
      "music":"cool" 
     }, 

]},{ 
"id":2, 
"name": "Leanne Graham", 
"username": "Bret", 
"email": "[email protected]", 
"address": { 
    "street": "Kulas Light", 
    "suite": "Apt. 556", 
    "city": "Gwenborough", 
    "zipcode": "92998-3874", 
    "geo": { 
    "lat": "-37.3159", 
    "lng": "81.1496" 
    } 
}, 
"phone": ["1-770-736-8031 x56442", "4087917884", "4089088939"], 
"website": "hildegard.org", 
"dept":[{"name":"divya", "address":"abc"},{"name":"divya1", "address":"abc1"}], 
"hobbies":[ 
      { 
      "books":"fiction", 
      "sports":"football", 
      "music":"rock" 
      }, 

      { 
      "books":"action", 
      "sports":"cricket", 
      "music":"jazz" 
      }, 
      { 
      "books":"action", 
      "sports":"cricket", 
      "music":"cool" 
      }, 
]}] 

我想只有在爱好关键只使用特定按键。假设我只需要爱好钥匙的书籍和体育项目。我如何在Nodejs中做到这一点? 结果应该是象下面这样:

[{ 
"id": 1, 
"name": "Leanne Graham", 
"username": "Bret", 
"email": "[email protected]", 
"address": { 
    "street":"201 S 4th St.", 
    "suite": "Apt. 556", 
    "city": "Gwenborough", 
    "zipcode": "92998-3874", 
    "geo": { 
    "lat": "-37.3159", 
    "lng": "81.1496" 
    } 
}, 
"company": { 
    "name": "Romaguera-Crona", 
    "catchPhrase": "Multi-layered client-server neural-net", 
    "bs": "harness real-time e-markets" 
}, 
"hobbies":[ 
     { 
      "books":"fiction", 
      "sports":"football", 

     }, 

     { 
      "books":"action", 
      "sports":"cricket", 

     }, 
     { 
      "books":"action", 
      "sports":"cricket", 

     }, 

] },{ 
"id":2, 
"name": "Leanne Graham", 
"username": "Bret", 
"email": "[email protected]", 
"address": { 
    "street": "Kulas Light", 
    "suite": "Apt. 556", 
    "city": "Gwenborough", 
    "zipcode": "92998-3874", 
    "geo": { 
    "lat": "-37.3159", 
    "lng": "81.1496" 
    } 
}, 
"phone": ["1-770-736-8031 x56442", "4087917884", "4089088939"], 
"website": "hildegard.org", 
"dept":[{"name":"divya", "address":"abc"},{"name":"divya1", "address":"abc1"}], 
"hobbies":[ 
      { 
      "books":"fiction", 
      "sports":"football", 

      }, 

      { 
      "books":"action", 
      "sports":"cricket", 

      }, 
      { 
      "books":"action", 
      "sports":"cricket", 

      }, 
]}]. 

请注意,这只是一个例子。我的输入是一个JSON数组和我想保留的键。这个例子的键是“ID”,“名称”,“hobbies.books”,“hobbies.sports”等。

+0

无论如何,你需要学习如何访问数组元素和对象属性。这归结为对JavaScript基础知识的基本缺乏理解。 – PHPglue

回答

0

鉴于您使用节点,我不会重新发明轮子。所以你可以使用像lodash pick函数,以及map。例如:

const filteredHobbies = theObject.hobbies.map(item => _.pick(item, ['books', 'sports'])) 
0

您可以使用Array.map()

hobbies.map(function(hobby) { 
    return { 
     books: hobby.books, 
     sports: hobby.sports, 
    } 
}); 

这是在创建一个新的数组,其中包含您在该函数中专门返回的值。

如果您使用ES6,你也可以胖箭头语法和其他“捷径”

hobbies.map((hobby) => ({ 
    books: hobby.books, 
    sports: hobby.sports, 
}); 

只需运行要操作任何阵列。假设你的对象被命名为json,你的函数看起来像:

const json = [] // your existing JSON 

const filtered = json.map((obj) => ({ 
    id: obj.id, 
    name: obj.name, 
    hobbies: obj.hobbies.map((hobby) => ({ 
     books: hobby.books, 
     sports: hobby.sports, 
    })), 
})); 
+0

感谢您的回答,但问题是我不知道关键的名字是爱好。它可以是存储在我的变量中的任何东西。所以我需要一个通用的解决方案,我的JSON数组放在一个变量中,另一个变量或数组包含值hobbies.books和hobbies.sports – realcodes

+0

它是如何确定你需要什么键的?这是你编造的东西,还是你正在使用的API? –

+0

是的,我有一个外部的API,为我提供json数据,并根据用户对密钥的选择,我必须仅显示数据 – realcodes

0

你也能开出的兴趣爱好数组,然后:

for(var h of hobbies) { 
    delete h.music; 
} 

最终的代码可能看起来像什么:

var hob = object.hobbies; 
for(var h of hob) { 
    delete h.music; 
} 
object.hobbies = hob;