I ask you for your any help with the following problem. Thank you!
Assume that you are given a nested array with three slots. For example,
var numbers = [1, [2,[3,4,5],[6,7,8]], [9,10,11]]
As you may notice, each array has three slots whether the slots are occupied by an integer or another array with three slots.
[{"key1":1,"key2":"nothing","key3":[9,10,11]},{"key1":2,"key2":"nothing","key3":[6,7,8]},{"key1":9,"key2":"nothing","key3":11}]
My goal is roughly to list all the atomic arrays the given in the nested array and turn them into associative array. For example,
[a,[b,c,d],e] -> [{key1: a, key2: X, key3: e}, {key1: b, key2: c, key3: d}]
If the element of an array is another array, the element might be given a name like "X".
var arr = new Array();
Array.prototype.node = function(){
if (this.length == 1){
arr.push({
key1: this[0],
})
}
else if (this.length == 2 ){
arr.push({
key1: this[0],
key2: this[1],
})
}
else if (this.length == 3){
arr.push({
key1: this[0],
key2: "nothing",
key3: this[2],
})
}
}
Array.prototype.mkk = function (){
switch(true){
case(!this[0].isArray): {this.node()}
case(this[1].length > 1): {this[1].node()}
case(this[2].length > 1): {this[2].node()}
}
return arr
}
function myFunction() {
var numbers = [1, [2,[3,[4,5,[]]], [4,[]]], [5,[]]]
//do something like ... JSON.stringify(numbers.mkk())
}