In my current project, I am trying to connect the array elements from foo3.flat() with the elements in foo2. For instance, if the 0th index in foo2 is 'address1' and the 0th index of foo3 is [1,2,3], all the individual numbers in [1,2,3] should be associated with 'address1'. This is how obj is currently mapped out.
Value of obj
{
address1: [ 1, 2, 3 ],
address2: [ 4, 5 ],
address3: [ 6, 7, 8, 9 ],
address4: [ 10 ]
}
Code
var foo1 = []
var foo2 = [
'address1',
'address2',
'address3',
'address4'
]
var foo3 = [
[1, 2, 3],
[4, 5],
[6, 7, 8, 9],
[10]
]
var foo4 = [
[11, 12, 13],
[14, 15],
[16, 17, 18, 19],
[]
]
var foo5 = [
[
[111, 111, 111],
[222, 222, 222],
[333, 333, 333]
],
[
[444, 444, 444],
[555, 555, 555]
],
[
[666, 666, 666],
[777, 777, 777],
[888, 888, 888],
[999, 999, 999]
],
[]
]
var obj = {}
foo2.forEach((key, i) => obj[key] = foo3[i])
foo3 = foo3.flat()
foo4 = foo4.flat()
foo5 = foo5.flat()
for (var i = 0; i < foo3.length; i++) {
foo1.push({
key1: '',
key2: foo3[i],
key3: foo4[i],
key4: foo5[i]
})
}
console.log(foo1)
Result
[
{ key1: '', key2: 1, key3: 11, key4: [ 111, 111, 111 ] },
{ key1: '', key2: 2, key3: 12, key4: [ 222, 222, 222 ] },
{ key1: '', key2: 3, key3: 13, key4: [ 333, 333, 333 ] },
{ key1: '', key2: 4, key3: 14, key4: [ 444, 444, 444 ] },
{ key1: '', key2: 5, key3: 15, key4: [ 555, 555, 555 ] },
{ key1: '', key2: 6, key3: 16, key4: [ 666, 666, 666 ] },
{ key1: '', key2: 7, key3: 17, key4: [ 777, 777, 777 ] },
{ key1: '', key2: 8, key3: 18, key4: [ 888, 888, 888 ] },
{ key1: '', key2: 9, key3: 19, key4: [ 999, 999, 999 ] },
{ key1: '', key2: 10, key3: undefined, key4: undefined }
]
Expected Result
[
{ key1: 'address1', key2: 1, key3: 11, key4: [ 111, 111, 111 ] },
{ key1: 'address1', key2: 2, key3: 12, key4: [ 222, 222, 222 ] },
{ key1: 'address1', key2: 3, key3: 13, key4: [ 333, 333, 333 ] },
{ key1: 'address2', key2: 4, key3: 14, key4: [ 444, 444, 444 ] },
{ key1: 'address2', key2: 5, key3: 15, key4: [ 555, 555, 555 ] },
{ key1: 'address3', key2: 6, key3: 16, key4: [ 666, 666, 666 ] },
{ key1: 'address3', key2: 7, key3: 17, key4: [ 777, 777, 777 ] },
{ key1: 'address3', key2: 8, key3: 18, key4: [ 888, 888, 888 ] },
{ key1: 'address3', key2: 9, key3: 19, key4: [ 999, 999, 999 ] },
{ key1: 'address4', key2: 10, key3: undefined, key4: undefined }
]
var foo1 = []
var foo2 = [
'address1',
'address2',
'address3',
'address4'
]
var foo3 = [
[1, 2, 3],
[4, 5],
[6, 7, 8, 9],
[10]
]
var foo4 = [
[11, 12, 13],
[14, 15],
[16, 17, 18, 19],
[]
]
var foo5 = [
[
[111, 111, 111],
[222, 222, 222],
[333, 333, 333]
],
[
[444, 444, 444],
[555, 555, 555]
],
[
[666, 666, 666],
[777, 777, 777],
[888, 888, 888],
[999, 999, 999]
],
[]
]
var obj = {}
foo2.forEach((key, i) => obj[key] = foo3[i])
foo3 = foo3.flat()
foo4 = foo4.flat()
foo5 = foo5.flat()
for (var i = 0; i < foo3.length; i++) {
foo1.push({
key1: '',
key2: foo3[i],
key3: foo4[i],
key4: foo5[i]
})
}
console.log(foo1)