从数组中的一个对象到另一个对象的添加/合并元素

时间:2018-12-01 16:17:35

标签: javascript arrays mongodb mongoose ecmascript-6

我的异步函数中有以下代码

async function test () {
    try {
        const aucs = await auctions.find({owner: 'owner', place: 'place'}).limit(15);
        const item = await Promise.all(aucs.map(async aucs => {
            const map = await items.find({id: aucs.item});
            return map[0]
        }));
        --->we are here [1]
    } catch (err) {
        console.log(err);
    }
}

test();

和点[1]我有两个可用的数组,其中包含另一个对象(都是Mongo的响应),它们是:

aucs = [ { _id: 5c00faa4936359120ceb3632,
    auc: 177215422,
    item: 130251,
    price: 26000000,
    lastModified: 1543567955000,
    date: 2018-11-30T08:53:56.290Z,
    __v: 0 },
    { _id: 5c00faa4936359120ceb363f,
    auc: 177215440,
    item: 130251,
    price: 26000000,
    lastModified: 1543567955000,
    date: 2018-11-30T08:53:56.290Z,
    __v: 0 },... ]

item = [ { _id: 5bcd8a6134cdd1223cd3239b,
    id: 130251,
    name: 'TEST_NAME_1',
    __v: 0 },
    { _id: 5bcd8a6134cdd1223cd3239b,
    id: 130252,
    name: 'TEST_NAME_2',
    __v: 0 },...]

我想将aucs中的每个元素aucs[i]item[i].name)添加到name: 'TEST_NAME_1' 喜欢:

combined = [ { _id: 5c00faa4936359120ceb3632,
        auc: 177215422,
        item: 130251,
        name: 'TEST_NAME_1',
        price: 26000000,
        lastModified: 1543567955000,
        date: 2018-11-30T08:53:56.290Z,
        __v: 0 },...]

我正在尝试将forauc[i].name = item[i].name一起使用aucs.push()循环,但由于某些未知原因,它不适用于我。 我收到.push is not a function的错误,并且for循环未添加任何内容。所以也许有人有主意吗?

  

注意:1

实际上解决了项目的一个问题,mongo在[ [ { ...} ] ]之类的数组中返回了我的数组,所以我应该使用return map [0]对其进行修复。

  

注意:2

auc和item都是根据typeof的对象,并且具有.length选项(它们的长度相同,并且应该一直存在。因此它们不是承诺的

  

注意:3

let merged = {...aucs, ...item};返回我

{ '0': { _id: 5bcd8a6134cdd1223cd3239b,
     id: 130251,
     name: 'JewelCraft',
     icon: 'inv_jewelcrafting_70_jeweltoy',
     stackable: 1,
     isAuctionable: true,
     __v: 0 }...

但不是我需要的

2 个答案:

答案 0 :(得分:1)

如果我正确理解,我们的目的是创建一个aucs的新集合,就像找到的一样,每个集合都进行更新以包括item集合中的项目名称,其中该项目具有匹配的ID。

let aucs = [ { _id: "5c00faa4936359120ceb3632",
    auc: 177215422,
    item: 130251,
    price: 26000000,
    lastModified: 1543567955000,
    date: "2018-11-30T08:53:56.290Z",
    __v: 0 },
    { _id: "5c00faa4936359120ceb363f",
    auc: 177215440,
    item: 130251,
    price: 26000000,
    lastModified: 1543567955000,
    date: "2018-11-30T08:53:56.290Z",
    __v: 0 } ];
 
item = [ { _id: "5bcd8a6134cdd1223cd3239b",
    id: 130251,
    name: 'TEST_NAME_1',
    __v: 0 },
    { _id: "5bcd8a6134cdd1223cd3239b",
    id: 130252,
    name: 'TEST_NAME_2',
    __v: 0 }];
    
// create a new collection of aucs modified to include the names of matching items
let combined = [];
aucs.forEach(auc => {
    let combinedAuc = Object.assign({}, auc);
    combined.push(combinedAuc);
    let matchingItem = item.find(i => auc.item === i.id);
    if (matchingItem) combinedAuc.name = matchingItem.name
});

console.log(combined)

答案 1 :(得分:1)

如果您在此处使用一些汇总技巧,将会更加优越,更快

    for (Integer integer : arr) {
        if (integer==2) {
            arr.set(1, 6);
        }
        System.out.println(integer);
    }
    System.out.println(arr);
相关问题