0

I have an array of objects of the structure coming from server response of iterated array object like as sample

var arrObj1  = [
    {"id":"1","value":21, "value1":13},
    {"id":"2","value":21, "value1":13 },
    ......n
    ]; 

var arrObj2 = [
    {"id":"1","value3":21, "value14":13},
    {"id":"2","value3":21, "value4":13 },
    ......n
    ]; 

var arrObj3 = [
    {"id":"1","value5":21, "value6":13},
    {"id":"2","value5":21, "value6":13 },
    ......n
    ]; 

But now I want to append the array values inot single new array according to key following structure of as iterated values of above array Expected Output:

var finalObj = [
        {
            "id" :  1
            "value" : 21,
            "value1" : 13,
            "value3" : 21,
            "value4" : 13,
            "value5" : 21,
            "value6" : 13,
        },
        {
            "id" :  2
            "value" : 21,
            "value1" : 13,
            "value3" : 21,
            "value4" : 13,
            "value5" : 21,
            "value6" : 13,
        },
        .....n

];
  • 3
    Hi and welcome to StackOerflow. Could you provide some more information about what you have tried already? What did not work? What error messages did you encounter? I am asking because it currently seems like you want others to do the coding work for you, which is not really the purpose of StackOverflow :) – David Losert Aug 07 '20 at 11:41
  • Look away to [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) – Nikita Madeev Aug 07 '20 at 11:43
  • @NikitaMadeev flatmap is enough – EugenSunic Aug 07 '20 at 11:45
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](https://stackoverflow.com/q/11922383/4642212) and use the available [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_the_Object_constructor) and [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods) methods. – Sebastian Simon Aug 07 '20 at 11:45
  • i tried using concat() but it seem not getting expected output – Prince Shukla Aug 07 '20 at 11:49
  • @PrinceShukla please check my answer – EugenSunic Aug 07 '20 at 12:01

4 Answers4

1

Push you arrays to a new array (you have to have your sub arrays in other list to loop through them) and then use flat, after that group your object according to the id property

var arrObj1 = [{
    "id": "1",
    "value": 21,
    "value1": 13
  },
  {
    "id": "2",
    "value": 21,
    "value1": 13
  },
];

var arrObj2 = [{
    "id": "1",
    "value3": 21,
    "value14": 13
  },
  {
    "id": "2",
    "value3": 21,
    "value4": 13
  },
];

var arrObj3 = [{
    "id": "1",
    "value5": 21,
    "value6": 13
  },
  {
    "id": "2",
    "value5": 21,
    "value6": 13
  },
];


const input = [];
input.push(arrObj2, arrObj3);
const preResult = input.flat();
result = preResult.reduce((acc, x) => {
  const index = acc.findIndex(y => y.id === x.id)
  if (index >= 0) {
    acc[index] = {
      ...acc[index],
      ...x
    }

  } else {
    acc.push(x)
  }
  return acc;
}, arrObj1)

console.log(result)
Ram
  • 117
  • 1
  • 1
  • 10
EugenSunic
  • 13,162
  • 13
  • 64
  • 86
1

You can use reduce for concated arrays

const arrObj1 = [
    { id: '1', value: 21, value1: 13 },
    { id: '2', value: 21, value1: 13 },
];
const arrObj2 = [
    { id: '1', value3: 21, value14: 13 },
    { id: '2', value3: 21, value4: 13 },
];
const arrObj3 = [
    { id: '1', value5: 21, value6: 13 },
    { id: '2', value5: 21, value6: 13 },
];

const result = [...arrObj1, ...arrObj2, ...arrObj3].reduce(
    (acc, { id, ...rest }) => ({ ...acc, [id]: acc[id] ? { ...acc[id], ...rest } : { ...rest } }),
    {},
);

console.log(Object.entries(result).map(([id, data]) => ({ id, ...data })));
Nikita Madeev
  • 4,284
  • 9
  • 20
0

You can iterate over array-length and push here for every entry 1 entry to the result. For getting this entry take a new object with the id and iterate over all (3 or perhaps more) arrays. For every array take the i-th entry and push for every key an entry with key:value (except for the key id itself).

Remark: You can use as much arrays you want and every object could contain as much value-prperties as you need. The only restriction is that every array has the same count of objects.

var arrObj1  = [
    {"id":"1","value":21, "value1":13},
    {"id":"2","value":21, "value1":13 }
    ]; 

var arrObj2 = [
    {"id":"1","value3":21, "value4":13},
    {"id":"2","value3":21, "value4":13 }
    ]; 

var arrObj3 = [
    {"id":"1","value5":21, "value6":13},
    {"id":"2","value5":21, "value6":13 }
    ]; 
    
let result =[];
let arrayAll = [arrObj1, arrObj2, arrObj3];

for (let i=0; i<arrayAll[0].length; i++) {

  let obj = arrayAll[0][i];
  let res = { id: obj.id};

  
  for (let j=0; j<arrayAll.length; j++) {
      let obj = arrayAll[j][i];
      Object.keys(obj).forEach(key => {
          if (key!='id') res[key] = obj[key];
      })
  }
  result.push(res);
}
console.log(result);
Sascha
  • 4,576
  • 3
  • 13
  • 34
0

Since you are using id to merge multiple objects, Map is one of the good option to merge.

let arrObj1  = [
    {"id":"1","value":21, "value1":13},
    {"id":"2","value":21, "value1":13 },
    ]; 

let arrObj2 = [
    {"id":"1","value3":21, "value14":13},
    {"id":"2","value3":21, "value4":13 },
    ]; 

let arrObj3 = [
    {"id":"1","value5":21, "value6":13},
    {"id":"2","value5":21, "value6":13 },
    ]; 

let mergedArray = [...arrObj1,...arrObj2,...arrObj3].reduce((acc,curr) =>{
    if(acc.has(curr.id)){
      acc.set(curr.id,  {...acc.get(curr.id),...curr});
    }else{
    acc.set(curr.id,curr);
    }
  return acc;
},new Map())
console.log(Array.from(mergedArray,x=>x[1]));
Ram
  • 117
  • 1
  • 1
  • 10