Question: I have around 20 objects like this one below,

```
{ 
       isReadOnly: false
       isRequired: false
       isResettable: false
       name: "test1"
},
{ 
       isReadOnly: false
       isRequired: false
       isResettable: false
       name: "test2"
}

```

I need to merge them into new 10 object, where

0, 1 -> 0 {0: data0, 1: data1}

2, 3 -> 1 {0: data2, 1: data3}

4, 5 -> 2 {0: data4, 1: data5}

x
x

18, 19 -> 9 {0, data18, 1: data19}

20 -> {0: data20, 1: null (or do not exist)}

How can I do it?

Answer: 
If you have array of objects like [{data0}, {data1}, {data3},...,{data19}]
and you want to make array with 10 object then you can acieve this like:

```
let oldArr = [{data0}, {data1}, {data3},...,{data19}]
let newArr = []
for (let i=0; i < oldArr.length; i=i+2) {
  let obj = {0: oldArr[i], 1: oldArr[i+1]}
  newArr.push(obj);
}

```
Basically, the proposed solution works as the previous ones, but there is a small difference. Let's say the `mainData` contains 21 objects, the last index `i` would be **20**, as we tend to access `mainData[21]` it would rise an error since the index 21 does not exist. Hence, I always check whether the next object is the last one or not.

```
const mainData = [...yourObjects];

let mergedArray = [];

const size = mainData.length;

for (let i=0; i < size; i+2) {
   mergedArray.push({
      0: mainData[index],
      1: (index + 2 <= size) ? mainData[index+1]: null
   });

}

```