我有一个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”等。
无论如何,你需要学习如何访问数组元素和对象属性。这归结为对JavaScript基础知识的基本缺乏理解。 – PHPglue