将JSON数组转换为选定的键' JSON数组

时间:2017-04-03 23:16:24

标签: javascript arrays json node.js underscore.js

我有一个JSON数组如下:

[{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"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": "Sincere@april.biz",
"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"
          },
]}]

我只想在Hobbies键中使用特定键。让我们说我只需要爱好关键的书籍和运动。我怎样才能在Nodejs中做到这一点? 结果应如下所示:

[{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"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": "Sincere@april.biz",
"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"," name"," hobbies.books"," hobbies.sports"等

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您可以使用Array.map()

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

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

如果您正在使用ES6,您还可以使用fat-arrow语法和其他“快捷方式”

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,
    })),
}));

答案 2 :(得分:0)

你可以开出爱好阵列然后:

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

最终代码看起来像是:

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