0

I need to convert a nested array into 2D in javascript, somewhat similar to the question answered for python at link

How to convert 2d nested array into 2d array single?

For example, the array

[[[[[[[[
  [16,12],[16,13],[16,14]]
 ],
 [[[[[[
   [46,42],[46,43]
 ]]]]],[
   [62,58],[62,59],[62,60]
 ]]]]]],
  [103,102]],[[118,114],[118,115],[118,116]]
]

needs to be converted to

[[16,12],[16,13],[16,14],[46,42],[46,43],[62,58],[62,59],[62,60],[103,102],[118,114],[118,115],[118,116]]

Please help, thanks in advance

This is what I tried, finally works after many trials :


function removeNestArray2D(object) {
    var result = [];
    if (Array.isArray(object)) { // check if object is valid array
        for(var i=0; i<object.length; i++) { 
            if(!Array.isArray(object[i])) { // check is each of array element is a valid array
                return object;
            }
            else {
                var tmp = removeNestArray2D(object[i]);

                if(tmp.length == 1) {
                    result = tmp[0];
                }
                else if (tmp.length == 2 && Number.isInteger(tmp[0]) && Number.isInteger(tmp[1])) {
                    result.push(tmp);
                }
                else {
                    for (var j=0; j<tmp.length; j++) {
                        result.push(tmp[j]);
                    }
                }
            }
        }
    }
    return result;
}
AKP
  • 13
  • 2
  • [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting), [What topics can I ask about here?](https://stackoverflow.com/help/on-topic), [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask), [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users/261593#261593) – Andreas Apr 22 '20 at 16:09
  • hey @AKP - what have you tried so far? And could you please edit your post so that your arrays and code are "code formatted"? It'll make it a lot easier to read. – SethGoodluck Apr 22 '20 at 16:13
  • @SethGoodluck I have posted my code, thanks. Formatting rules noted. – AKP Apr 22 '20 at 19:19

2 Answers2

0

Recursive approach will help here. Check each array item if there are size 2 and both are number values then push to result array otherwise continue iteration recursively.

const arr = [[[[[[[[
  [16,12],[16,13],[16,14]]
 ],
 [[[[[[
   [46,42],[46,43]
 ]]]]],[
   [62,58],[62,59],[62,60]
 ]]]]]],
  [103,102]],[[118,114],[118,115],[118,116]]
];

const get2dArray = arr => {
  const res = [];
  const pushRecursive = arr => {
    if (arr.length == 2 && arr.every(x => Number.isInteger(x))) {
      res.push(arr);
    } else {
      arr.forEach(pushRecursive);
    }
  };
  pushRecursive(arr);
  return res;
};

console.log(get2dArray(arr));
Siva K V
  • 10,561
  • 2
  • 16
  • 29
0

function removeNestArray2D(object) {
    var result = [];
    if (Array.isArray(object)) { // check if object is valid array
        for(var i=0; i<object.length; i++) { 
            if(!Array.isArray(object[i])) { // check is each of array element is a valid array
                return object;
            }
            else {
                var tmp = removeNestArray2D(object[i]);

                if(tmp.length == 1) {
                    result = tmp[0];
                }
                else if (tmp.length == 2 && Number.isInteger(tmp[0]) && Number.isInteger(tmp[1])) {
                    result.push(tmp);
                }
                else {
                    for (var j=0; j<tmp.length; j++) {
                        result.push(tmp[j]);
                    }
                }
            }
        }
    }
    return result;
}


AKP
  • 13
  • 2