Array of objects structuring and key replacing

问题内容:

I have an array of objects like this (Data):

[
  {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},
]

I have another array of object like this (UserData):

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

What i want is an object like this:

{
  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}]
}

I know I can restructure and make an object with the Data as the array and the authorID as the key like this:

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

But how do i also replace the authorId with with the full name from the UserData array?? Working with objects and arrays like this is hard..

问题评论:

3  
please add some working data for testing.

答案:

答案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);

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:

You can use reduce(), filter() and map() methods to get desired result.

var data = [
  {content: '1someString', tag: 'someString', data: 'someString', author: 1},
  {content: '2someString', tag: 'someString', data: 'someString', author: 2},
  {content: '3someString', tag: 'someString', data: 'someString', author: 2},
  {content: '4someString', tag: 'someString', data: 'someString', author: 1},
]

var users = [
  {author: 1, firstName: 'aFirst', lastName: 'aLast'},
  {author: 2, firstName: 'bFirst', lastName: 'bLast'},
]

var result = users.reduce(function(r, e) {
  var name = `${e.firstName} ${e.lastName}`;
  r[name] = data.filter(a => a.author == e.author)
  .map(({content, tag, data}) => ({content, tag, data}))
  return r;
}, {})

console.log(result)

答案评论:

答案3:

You could take a Map for keeping the same reference to an array for each author.

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; }

答案评论:

原文地址:

https://stackoverflow.com/questions/47753237/array-of-objects-structuring-and-key-replacing

添加评论

友情链接:蝴蝶教程