0

I have nested array like this,

var a=[1,2,[3,4,[5,6]]];

I am trying to convert it to flatten array.

var a=[1,2,[3,4,[5,6]]];
var b = a.join(',');
console.log(b);
var c = JSON.parse("[" + b + "]")
console.log(typeof c);

I used join and JSON.parse() methods, I am able to convert to a flat array, but when I do typeof for the array 'c', it says the type as 'object but not an array. My question is as everything is considered as an 'Object' in Javascript. Is returning the array 'c' as 'object' is correct or not?

Sandhya3478
  • 47
  • 2
  • 10
  • Use flat: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat – Get Off My Lawn Mar 01 '19 at 03:07
  • Possible duplicate of [Why does typeof array with objects return "Object" and not "Array"?](https://stackoverflow.com/questions/12996871/why-does-typeof-array-with-objects-return-object-and-not-array) – Get Off My Lawn Mar 01 '19 at 03:13

4 Answers4

3

Arrays are a type of object which is why it is printing object to the console. To test if an array is an array you need to use Array.isArray(someVar).

Note: You can use flat() to flatten the array.

let a = [1,2,[3,4,[5,6]]]

// 2 is the depth
console.log(a.flat(2))

console.log(typeof a)

// Prints true
console.log(Array.isArray(a))
// Prints false
console.log(Array.isArray({a:123}))
// Prints false
console.log(Array.isArray(123))
// Prints false
console.log(Array.isArray('Kitty'))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

The best method you can use to check if c is an array is Array.isArray(), and it gives true for c so you are "safe":

var a=[1,2,[3,4,[5,6]]];
var b = a.join(',');
var c = JSON.parse("[" + b + "]");

console.log(c);
console.log("c is Array?: " + Array.isArray(c));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

And yes, it is ok the typeof for an array gives object because from MDN:

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations...

var a=[1,2,[3,4,[5,6]]];
console.log(typeof a);

var b = new Array(10);
console.log(typeof b);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Now, if you need to flatten your array, you can use the experimental Array.flat() it takes and optional deep argument (default to 1). However note this method can not be available on some browser/version. In this, can you can use the next version with recursion:

const flattenDeep = (arr) =>
{
   return arr.reduce(
       (acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val),
       []
   );
}

console.log(flattenDeep([1,2,[3,4,[5,6]]]));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Shidersz
  • 16,846
  • 2
  • 23
  • 48
0

You can recursively flatten the array with reduce like this. If the item inside the array is an array, flatten the array with the same function.

const flatten = (arr) => {
  return arr.reduce((acc,item) => {
    if (Array.isArray(item)) {
      return acc.concat(flatten(item))
    }
    return acc.concat(item)
  }, [])
}

const a = [1,2,[3,4,[5,6]]];
console.log(flatten(a))
Prasanna
  • 4,125
  • 18
  • 41
0

Try:

obj instanceof Array

Also something to consider -- if you're passing the Array across frame boundaries, you will need to do the following:

Object.prototype.toString.call(obj) === '[object Array]'

See related post: How to detect if a variable is an array

Chris Lacaille
  • 137
  • 1
  • 5