Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge every to 2 row in loop to one

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}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

const arr = [...Array(20).keys()].map((_, index) => ({ 
       isReadOnly: false,
       isRequired: false,
       isResettable: false,
       name: index
}));

let combinedArr = [];

for(let i = 0; i < arr.length; i+=2){
    combinedArr.push({0: arr[i], 1: arr[i + 1]})
}

console.log(combinedArr);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading