This is my array. I have multiple objects in it, some having values undefined
.
var array = [
{ 0: undefined, 1: 32, 2: "four" },
{ 0: undefined, 1: undefined, 2: "three" },
{ 0: undefined, 1: 24, 2: "two" },
{ 0: 14, 1: 24, 2: "five" },
{ 0: 10, 1: 21, 2: "one" },
]
I want to check if the array objects have undefined value and if they have any undefined value the next objects should be appended.
Example In the first element {0: undefined, 1: 32, 2: "four"}
I have 0:undefined
the second {0: undefined, 1: undefined, 2: "three"}
element should replace this and new object should form which is {0: {0: undefined, 1: undefined, 2: "three"}, 1: 32, 2: "four"}
.
I know the Array.forEach
can do this. I can do if it was array like by using Array.splice()
and Array.lastIndexOf()
. Like first search from right side or last undefined value and append the next array in it then do it again again.
The final array must be like :
{
0: {
0: {
0: 10,
1: 21,
2: "one"
},
1: {
0: {
0: 14,
1: 24,
2: "five"
},
1: 24,
2: "two"
},
2: "three"
},
1: 32,
2: "four"
}