【问题标题】:Merging objects in node.js在 node.js 中合并对象
【发布时间】:2018-07-31 05:34:25
【问题描述】:

我有一些对象试图在 Node.js 中将它们合并在一起 第一个对象是这样的格式

{ ids : [id, id, id, ....]}

对象的其余部分是这样的格式

{ postcode: '' , lat: '', lon: ''}, {postcode: '' , lat: '', lon: ''},....

我需要以一种类似于此的格式合并它们

{ id: '', postcode: '', lat: '', lon:''}

我想知道是否可以在 nodejs 中做到这一点?

【问题讨论】:

  • 数据是否在同一个索引下?你想得到带有idlat的新对象,lon没有postcode吗?
  • @NinaScholz 是的,它们是相关的。所以我想将数组中的每个 id 添加到一个对象中。例如,数组的第一个索引处的 id 到第一个对象,数组的第二个索引处的 id 到第二个对象等等。最终我将包含多个对象或单个数组 {id: , postcode:, lat:, lon:,}....
  • @ScottSauyet 我尝试循环遍历它们并合并它们,但出于某种原因,我要么错过了一个对象,要么数据永远不匹配。问题是我是 java 开发人员,现在使用 node 和它的异步特性真的让我很困惑!
  • 出于好奇,这是要将ids 的查询与数据库中的results 数组合并吗?如果是这样,根据数据库,如果您调整查询参数,对象可能已经包含 id
  • 重点是 you really should 在你的问题中包含一些关于你到目前为止所做的事情的讨论,通常是通过显示一些代码。

标签: javascript node.js for-loop merge array-merge


【解决方案1】:

使用函数forEach

假设对象都在同一个数组中。

var array = [{ ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']}, { postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }];

var result = array.slice(-(array.length - 1)); // Create a new Array removing the first position.
result.forEach((e, i) => e['id'] = array[0].ids[i] /* Add id from the first element*/);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

分离的对象

这种方法修改了源数组。

var firstObject = { ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']};
var array = [{ postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }];

array.forEach((e, i) => e['id'] = firstObject.ids[i]);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

使用函数map

假设对象都在同一个数组中。

var array = [{ ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']}, { postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }];

var result = array.slice(-(array.length - 1)); // Create a new Array removing the first position.
result = result.map((e, i) => ({ ...e, ...{'id': array[0].ids[i] } }) /* Add id from the first element*/);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

分离的对象

var firstObject = { ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']};
var array = [{ postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }];

var result = array.map((e, i) => ({ ...e, ...{ 'id': firstObject.ids[i] } }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 这些答案很好,谢谢!但是,我在你的答案中的数组不在数组内,所以它就像{},{},{},当我将它推入数组时,我得到了我想要的结果,但它永远不会增加。因此,所有对象的 id 相同!任何想法如何增加它?
  • @M.Asgari 您能否分享您问题中的输入和输出,以便更好地了解您在寻找什么?
  • 所以这些是我的输入,{ postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' } { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' } { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' } 这是第一个输入然后我有另一个输入是 { ids: [ 5a6df69d84a1d08d9104c0b2, 5a6df69d84a1d08d9104c0b3, 5a6df69d84a1d08d9104c0b4]}
  • 我的输出应该是这两个合并的,比如{id(each one unique) : ' ' , postcode: ' ' , lat: ' ', lon: ' ' }......
  • @M.Asgari 输入在数组中?
【解决方案2】:

试试这个。 Object.assign 改变你的对象。

let idsObj = { ids : [id, id, id, ....]};

let postCodeArray = [{ postcode: '' , lat: '', lon: ''}, {postcode: '' , lat: '', lon: ''}];

postCodeArray.forEach((elem,index) => Object.assign(elem,{id: idsObj.ids[index]}));

【讨论】:

    【解决方案3】:

    您可以在对象前面加上id

    var object1 = { ids: [23, 42] },
        postcodes = [{ postcode: 'XY' , lat: '11', lon: '22' }, { postcode: 'ZY' , lat: '33', lon: '44'}],
        merged = postcodes.map((o, i) => Object.assign({ id: object1.ids[i] }, o));
        
    console.log(merged);

    【讨论】:

      【解决方案4】:

      ECMAScript 2015 (ES6) 标准方法

      Object.assign(obj1, obj2);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-23
        • 2020-04-21
        • 2019-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        相关资源
        最近更新 更多