I have an array of pairs of values somewhat like this:
let myarr = [[1, 'a'],
[2, 'b'],
[3, 'c']]
and one like this:
let otherArr = [5,6,7]
and I would like to convert them to an object which would be of the form:
{
"data":[
{
"id":5,
"pair":[
1,
"a"
]
},
{
"id":6,
"pair":[
2,
"b"
]
},
{
"id":7,
"pair":[
3,
"c"
]
}
]
}
As a first attempt
I tried to use a for loop and tried to create a list of the pair keys like this
for (let pair = 0; pair < myarr.length; pair++) {
myobj[pair].pair = myarr[pair]
and I get an error stating TypeError: Cannot set property 'pair' of undefined
Is there a efficient way to create a object like the example above Thanks in advance