Daniel
Daniel

Reputation: 1

Reformat array of objects

I am trying to reformat this array of objects:

let users = [{
        Username: "test1",
        Attributes: [{
            Name: "sub",
            Value: "1234"
        }, {
            Name: "email",
            Value: "[email protected]"
        }]
    },
    {
        Username: "test2",
        Attributes: [{
            Name: "sub",
            Value: "5678"
        }, {
            Name: "email",
            Value: "[email protected]"
        }]
    },
]

I want to reformat to this:

users: [{
            Username: "test1",
            sub: "1234",
            email: "[email protected]}, {
                    Username: "test2",
                    sub: "5678",
                    email: "[email protected]}]

How to reformat this array of the object? Thanks

Upvotes: 0

Views: 1860

Answers (3)

adiga
adiga

Reputation: 35253

You could loop through the users using for...of. Loop trough the Attributes to convert each object with 2 properties to a key-value pair. Create a merged object using Object.assign()

const users=[{Username:"test1",Attributes:[{Name:"sub",Value:"1234"},{Name:"email",Value:"[email protected]"}]},{Username:"test2",Attributes:[{Name:"sub",Value:"5678"},{Name:"email",Value:"[email protected]"}]}];

const output = [];

for(const { Username, Attributes } of users) {
  const attributes = Attributes.map(({ Name, Value }) => ({ [Name]: Value }));
  output.push(Object.assign({ Username }, ...attributes))
}

console.log(output)

Upvotes: 0

Shidersz
Shidersz

Reputation: 17190

One possible solution is to use Array.map() to map each original object to a new one. Inside the map() you can traverse the Attributes array to add properties into the new object that will be returned.

let users =[
  {
    Username: "test1",
    Attributes:[
      {Name: "sub", Value:"1234"},
      {Name:"email", Value:"[email protected]"}
    ]
  },
  {
    Username: "test2",
    Attributes: [
      {Name: "sub", Value:"5678"},
      {Name:"email", Value:"[email protected]"}
    ]
  }
];

let res = users.map(({Username, Attributes}) =>
{
    let newObj = {Username};    
    Attributes.forEach(({Name, Value}) => newObj[Name] = Value);
    return newObj;
});

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Upvotes: 0

Eddie
Eddie

Reputation: 26844

You can use map to loop thru the array. Use reduce to summarize the Attributes array into an object.

let users = [{"Username":"test1","Attributes":[{"Name":"sub","Value":"1234"},{"Name":"email","Value":"[email protected]"}]},{"Username":"test2","Attributes":[{"Name":"sub","Value":"5678"},{"Name":"email","Value":"[email protected]"}]}]

let result = users.map(({Username,Attributes}) => {
  let a = Attributes.reduce((c, v) => ({ ...c,[v.Name]: v.Value}), {});
  a.Username = Username;
  return a;
});

console.log(result);

Shorter Version:

let result = users.map(({Username, Attributes})=>({Username,...Attributes.reduce((c,v)=> ({...c, [v.Name]: v.Value}) ,{})}));

Upvotes: 2

Related Questions