0

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

3 Answers 3

2

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}) ,{})}));
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.