3

Arrays:

 array1 = [{id:1, name:"raju"},{id:2, name:"ravi"},{id:4, name:"john"},{id:6, name:"jack"}];
 array2= [{id:1, degree:"b.com"},{id:3, degree:"b.a"},{id:4, degree:"c.a"},{id:5, degree:"horticulture"}];
 array3= [{id:1, age:20},{id:3, age:21},{id:6, age:27},{id:7, age:25}];

Required result is:

resultarray = [
    {id:1, name: "raju", degree:"b.com",age:20},
    {id:2, name: "ravi"},
    {id:3, degree:"b.a", age:21},
    {id:4, name:"john", degree:"c.a"},
    {id:5, degree:"horticulture"},
    {id:6, name:"jack", age:27},
    {id:7, age:25}
 ] 

i have tried different functions and tried for two arrays but not able to merge the object which doesnt have id to compare..

Pavan Skipo
  • 1,757
  • 12
  • 29
  • hii, is there any alternative for object.values & object.keys.. because these two are not working in IE11.. is there any other way to get this output without using object.values & object.keys – Phani Kumar Devu Jun 11 '19 at 07:51

3 Answers3

7

You can use reduce and destructuring

Here idea is

  • First merge all the arrays in one array.
  • Now using reduce we create id as key in op object
  • If id key is already there we merge the inp and op[inp.id]
  • If id is not there we create a new key with value inp

let array1 = [{id:1, name:"raju"},{id:2, name:"ravi"},{id:4, name:"john"},{id:6, name:"jack"}];
let array2= [{id:1, degree:"b.com"},{id:3, degree:"b.a"},{id:4, degree:"c.a"},{id:5, degree:"horticulture"}];
let array3= [{id:1, age:20},{id:3, age:21},{id:6, age:27},{id:7, age:25}];

let temp = [...array1,...array2,...array3]

let op = temp.reduce((op,inp)=>{
  op[inp.id] = op[inp.id] || inp
  op[inp.id] = {...op[inp.id],...inp}
  return op
},{})

console.log(Object.values(op))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • can we keep this result array as a child to one parent id(another different id ) – Phani Kumar Devu Mar 22 '19 at 05:17
  • 1
    @PhaniKumarDevu i am unable to understand what you meant by above line, can you add explanation or some example ? – Code Maniac Mar 22 '19 at 05:25
  • i mean there will one more id like{ UserID: 201, Userrating:resultarray} here UserID is parent and Userrating is child.. – Phani Kumar Devu Mar 22 '19 at 05:28
  • 1
    You can simply set `userrating` key's value to `output` we are getting, `{userId: 201, userrating: Object.values(op)}` something like this – Code Maniac Mar 22 '19 at 05:29
  • hii, is there any alternative for object.values & object.keys.. because these two are not working in IE11.. is there any other way to get this output without using object.values & object.keys – Phani Kumar Devu Jun 11 '19 at 07:50
  • 1
    @PhaniKumarDevu you can use polyfill for that [Object.values ployfill](https://github.com/tc39/proposal-object-values-entries/blob/master/polyfill.js) – Code Maniac Jun 11 '19 at 17:01
2

a possible solution:

let array1 = [{id:1, name:"raju"},{id:2, name:"ravi"},{id:4, name:"john"},{id:6, name:"jack"}];
let array2= [{id:1, degree:"b.com"},{id:3, degree:"b.a"},{id:4, degree:"c.a"},{id:5, degree:"horticulture"}];
let array3= [{id:1, age:20},{id:3, age:21},{id:6, age:27},{id:7, age:25}];

let resp = [].concat(array1, array2, array3).reduce((acc, ele) => {
        let obj = acc.find(x => x.id === ele.id);
        return obj ? (Object.keys(ele).forEach(x => obj[x] = ele[x]), acc) : acc.concat(ele);
    }, [])
    
console.log(resp)
guijob
  • 4,413
  • 3
  • 20
  • 39
1

There will be better solutions than this, but this is what I've tried:

Merging the 3 arrays into one :

let array = [...array1, ...array2, ...array3]

Looping through the items and then putting the values to "object"

let object = {}    
array.forEach((item) => {
    object[item.id] = { ...object[item.id],...item}})

Final solution

result = Object.values(object)

object variable will contain

object = { '1': { id: 1, age: 20, name: 'raju', degree: 'b.com' },
      '2': { id: 2, name: 'ravi' },
      '3': { id: 3, age: 21, degree: 'b.a' },
      '4': { id: 4, degree: 'c.a', name: 'john' },
      '5': { id: 5, degree: 'horticulture' },
      '6': { id: 6, age: 27, name: 'jack' },
      '7': { id: 7, age: 25 } }

result variable will contain

result = [ { id: 1, age: 20, name: 'raju', degree: 'b.com' },
  { id: 2, name: 'ravi' },
  { id: 3, age: 21, degree: 'b.a' },
  { id: 4, degree: 'c.a', name: 'john' },
  { id: 5, degree: 'horticulture' },
  { id: 6, age: 27, name: 'jack' },
  { id: 7, age: 25 } ]
Pavan Skipo
  • 1,757
  • 12
  • 29