【问题标题】:How to get specific objects from json arrays using nodejs如何使用nodejs从json数组中获取特定对象
【发布时间】:2018-02-26 19:45:19
【问题描述】:

我有这个寻找的数组

{
  "arr": [
    {
      "obj": [
        {
          "reg_at": 1519615460064,
          "id": "367790083"
        },
        {
          "reg_at": 1519615460064,
          "id": "41370460"
        }
      ]
    },
    {
      "obj": [
        {
          "reg_at": 1519615460065,
          "id": "215109021"
        },
        {
          "reg_at": 1519615460065,
          "id": "72173002"
        }
      ]
    }
  ]
}

我想将所有 id 对象放入一个数组中,如下所示:

[367790083, 41370460, 41370460]

请问我该如何使用 nodejs 实现这一目标?我试过循环,但没有得到想要的输出。

感谢您的帮助。

【问题讨论】:

  • 您可以使用映射函数并为每个对象返回您需要的内容codeburst.io/…
  • 就像我说的,我尝试循环遍历数组并将 ID 推送到一个空数组,但没有得到所需的结果。
  • 我承认 OP 是初学者,但我认为这个问题没有任何问题,所以我猜投反对票是出于个人对 OP 的怨恨。
  • 你应该多研究一下这个。老实说,这是非常基本的,您可以通过更多挖掘轻松找到解决方案(以防您想知道为什么会被否决)。或者至少写下您尝试过的方法,我们可以告诉您您的方法存在什么问题。
  • 您可以使用lodash或下划线来实现这一点。

标签: javascript arrays json node.js


【解决方案1】:

使用reducemap

var output = obj.arr.reduce( (a, c) => 
    a.concat( c.obj.map(  
       s => +s.id )  )  , [])

说明

  • 使用reduce迭代和累积输出到a
  • 使用map返回obj数组内arr中每个项目的id(数字转换

演示

var obj = {
  "arr": [
    {
      "obj": [
        {
          "reg_at": 1519615460064,
          "id": "367790083"
        },
        {
          "reg_at": 1519615460064,
          "id": "41370460"
        }
      ]
    },
    {
      "obj": [
        {
          "reg_at": 1519615460065,
          "id": "215109021"
        },
        {
          "reg_at": 1519615460065,
          "id": "72173002"
        }
      ]
    }
  ]
};
var output = obj.arr.reduce( (a, c) => 
    a.concat( c.obj.map(  
       s => +s.id )  )  , []);
console.log( output );

【讨论】:

  • 非常感谢
【解决方案2】:

鉴于您是初学者,我将采用老式的方法让您了解数据的组织方式。

var data = {
  "arr": [
    {
      "obj": [
        {
          "reg_at": 1519615460064,
          "id": "367790083"
        },
        {
          "reg_at": 1519615460064,
          "id": "41370460"
        }
      ]
    },
    {
      "obj": [
        {
          "reg_at": 1519615460065,
          "id": "215109021"
        },
        {
          "reg_at": 1519615460065,
          "id": "72173002"
        }
      ]
    }
  ]
};

// The variable for the result
var result = [];

// "data" is the object and "arr" is an array and has a length
for (var i=0; i<data.arr.length; i++) {
    // For clarity, pick the current object from the array
    var theArrItem = data.arr[i];

    // That object then has a "obj" which is an array
    for (var j=0; j<theArrItem.obj.length; j++) {
        // Again, pick the current object
        var theObjItem = theArrItem.obj[j];
        // Add the id of the current object to the result
        result.push(theObjItem.id);
    }
}

console.log(result);

【讨论】:

    【解决方案3】:

    您可以简单地使用 lodash 库,因为它具有许多可以减少服务器端编码的内置函数。

    npm i --save lodash
    _.map(arr, 'id');
    

    结果将是 [367790083, 41370460, 41370460]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多