【问题标题】:convert JSON Array to selected keys' JSON Array将 JSON 数组转换为选定键的 JSON 数组
【发布时间】:2017-04-04 07:34:31
【问题描述】:

我有一个如下的 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 key 中的书籍和运动。我怎样才能在 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”等。

【问题讨论】:

  • 无论如何,您都需要学习如何访问数组元素和对象属性。这归结为对 JavaScript 基础知识缺乏基本了解。

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


【解决方案1】:

鉴于您使用的是节点,我不会重新发明轮子。所以你可以在map 旁边使用类似lodash pick 的函数。例如:

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

【讨论】:

    【解决方案2】:

    您可以使用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,
        })),
    }));
    

    【讨论】:

    • 感谢您的回答,但问题是我不知道关键名称是爱好。它可以是存储在我的变量中的任何内容。所以我需要通用解决方案,其中我的 JSON 数组在一个变量中,另一个变量或数组包含值 hobbies.books 和 hobbies.sports
    • 如何确定您需要哪些密钥?这是您编造的还是您正在关注的 API?
    • 是的,我有一个外部 API 可以为我提供 json 数据,并且根据用户选择的键,我必须只显示该数据
    【解决方案3】:

    你可以划分出爱好数组,然后:

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

    最终的代码可能看起来像这样:

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

    【讨论】:

      猜你喜欢
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2014-05-26
      • 2011-01-18
      相关资源
      最近更新 更多