结构和键替换的对象数组

时间:2017-12-11 12:42:31

标签: javascript arrays object

我有一个像这样的对象数组(数据):

[
  {content: someString, tag: someString, data: someString, author: someId},
  {content: someString, tag: someString, data: someString, author: someId},
  {content: someString, tag: someString, data: someString, author: someId},
  {content: someString, tag: someString, data: someString, author: someId},
]

我有另一个像这样的对象数组(UserData):

[
  {author: someId, firstName: someString, lastName: someString},
  {author: someId, firstName: someString, lastName: someString},
]

我想要的是这样一个对象:

{
  authorFullName: [{content: someString, tag: someString, date: someString},
                   {content: someString, tag: someString, date: someString}],
  authorFullName: [{content: someString, tag: someString, date: someString},
                   {content: someString, tag: someString, date: someString}]
}

我知道我可以使用Data作为数组重构并创建一个对象,并将authorID作为键,如下所示:

    const newObject = OldArrayOfObjects.reduce((result, item) => ({
     ...result,
     [item.author]: [
       ...(result[item.author] || []),
       { content: item.content, tag: item.tag, date: item.date },
     ],
   }), {});

但是我如何用UserData数组中的全名替换authorId?使用像这样的对象和数组很难..

3 个答案:

答案 0 :(得分:1)

您可以使用System.out.println((String)globalMap.get("tMysqlOutput_1_QUERY")); reduce()filter()方法获得所需的结果。

map()

答案 1 :(得分:1)

var dummy = [
    { content: 'sudo', tag: 'sudo', data: 'sudo', author: 1 },
    { content: 'sudo', tag: 'sudo', data: 'sudo', author: 2 },
    { content: 'sudo', tag: 'sudo', data: 'sudo', author: 1 },
    { content: 'sudo', tag: 'sudo', data: 'sudo', author: 2 },
];

var test = [
    { author: 1, firstName: 'ragav simha', lastName: 'ss' },
    { author: 2, firstName: 'ragav ', lastName: 'ragavmm ' },
];

const result = test.reduce((acc, item) => {
    const newArray = dummy.filter((elem) => {
        return elem.author == item.author
    });
    const username = `${item.firstName} ${item.lastName}`;
    acc[username] = newArray;
    return acc;
}, {});

console.log(result);

结果

  { 'ragav simhass': 
   [ { content: 'sudo', tag: 'sudo', data: 'sudo', author: 1 },
     { content: 'sudo', tag: 'sudo', data: 'sudo', author: 1 } ],
  'ragav ragavmm ': 
   [ { content: 'sudo', tag: 'sudo', data: 'sudo', author: 2 },
     { content: 'sudo', tag: 'sudo', data: 'sudo', author: 2 } ] }

答案 2 :(得分:1)

您可以使用Map为每个作者保留对数组的相同引用。

var data = [{ content: 'someString1', tag: '', data: 'someString1', author: 1 }, { content: 'someString2', tag: '', data: 'someString2', author: 2 }, { content: 'someString3', tag: '', data: 'someString3', author: 2 }, { content: 'someString4', tag: '', data: 'someString4', author: 1 }],
    authors = [{ author: 1, firstName: 'Jane', lastName: 'Goodall' }, { author: 2, firstName: 'John', lastName: 'Doe' }],
    map=new Map,
    result = Object.assign(...authors.map(({ author, firstName, lastName, values = [] }) => {
        map.set(author, values);
        return { [[firstName, lastName].join(' ')]: values };
    }));

data.forEach(o => map.get(o.author).push(o));

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