转换JSON阵列来选择键的JSON阵列
问题描述:
我有一个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”等。
答
鉴于您使用节点,我不会重新发明*。所以你可以使用像lodash pick
函数,以及map
。例如:
const filteredHobbies = theObject.hobbies.map(item => _.pick(item, ['books', 'sports']))
答
您可以使用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,
})),
}));
答
你也能开出的兴趣爱好数组,然后:
for(var h of hobbies) {
delete h.music;
}
最终的代码可能看起来像什么:
var hob = object.hobbies;
for(var h of hob) {
delete h.music;
}
object.hobbies = hob;
无论如何,你需要学习如何访问数组元素和对象属性。这归结为对JavaScript基础知识的基本缺乏理解。 – PHPglue