2

This is my initial data:

const data = [
   {
      "user":{
         "amount":"1",
         "date":"2020-07-31T18:34:48.635Z",
         "shiftSelected":"10:00"
      }
   },
   {
      "user":{
         "name":"Name",
         "surname":"aaa",         
         "obs":"eee"
      }
   }
]

I'm trying to turn an array of objects in unique array. This is my output:

const newData = {
  amount: "1",
  date: "2020-07-31T18:34:48.635Z",
  shiftSelected: "10:00",
  name: "Name",
  surname:"aaa",         
  obs:"eee"    
}

I can iterate over the array with a map call: let b = data.map(item => item.user), but I need to write more code to join then. I know that it's possible to do it with one unique logic. I tried but without successful.

4 Answers 4

2

You can use reduce with Object.assign to merge the properties of the objects. Note that with this method, later properties will overwrite previous ones.

const data = [
   {
      "user":{
         "amount":"1",
         "date":"2020-07-31T18:34:48.635Z",
         "shiftSelected":"10:00"
      }
   },
   {
      "user":{
         "name":"Name",
         "surname":"aaa",         
         "obs":"eee"
      }
   }
];
const result = data.reduce((acc,{user})=>Object.assign(acc,user), {});
console.log(result);

Object spread syntax will also work.

const data = [
   {
      "user":{
         "amount":"1",
         "date":"2020-07-31T18:34:48.635Z",
         "shiftSelected":"10:00"
      }
   },
   {
      "user":{
         "name":"Name",
         "surname":"aaa",         
         "obs":"eee"
      }
   }
];
const result = data.reduce((acc,{user})=>({...acc, ...user}), {});
console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!
@Zkk Happy to help.
1

const data = [
   {
      "user":{
         "amount":"1",
         "date":"2020-07-31T18:34:48.635Z",
         "shiftSelected":"10:00"
      }
   },
   {
      "user":{
         "name":"Name",
         "surname":"aaa",         
         "obs":"eee"
      }
   }
]

let b = data.reduce((acc, rec) => {
  const { user } = rec
  return { ...acc, ...user}
}, {} )

console.log(b)

Comments

0

use this after map over datas this is not a logic but.....

const x = data.map(obj => obj.user);

function flatObj(arr) {
  const res = {};
  arr.forEach(y => {
    for(const key in y) {
      res[key] = y[key];
    }
  })
  return res;
}

const resault = flatObj(x);

Comments

0

Lets say you have this array of objects. We can make it unique with reduce.

function unique(arr, key) {
    return arr.reduce((a, d) => {
        if (!a.find(ae => ae[key] === d[key]))
            a.push(d)
        return a;
    }, [])
}

let users = [
    {
        id: 0,
        name: 'name1',
        age: 10
    },
    {
        id: 1,
        name: 'name2'
    },
    {
        id: 1,
        name: 'name2'
    }
];

users = unique(users, 'id')

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.