1

I have an Array of Array and I just want to print outer Array not inner Array.

For example my array is :-

[
"Stories",
"Tasks",
"In Progress",
"In Review",
"Completed",
[
{
    "divName": "content-container2",
    "content": "us 2345",
    "topPos": 109,
    "leftPos": 150
},
{
    "divName": "content-container3",
    "content": "Description",
    "topPos": 98,
    "leftPos": 382
},
{
    "divName": "content-container4",
    "content": "12212",
    "topPos": 110,
    "leftPos": 644
}
]
]

I just want to show ["Stories", "Tasks", "In Progress", "In Review", "Completed"], nothing else.

Please suggest how to handle this thing in javascript?

user243405
  • 83
  • 7
Anand Deep Singh
  • 2,560
  • 3
  • 22
  • 28

4 Answers4

3

While iterating the array, check the type of each item in it like

for (var i =0; i< arr.length; i++) {
        if (typeof arr[i] === "string") {
          console.log(arr[i]);
        }
 }

A better approach (inspired from this answer)

for (var i =0; i< arr.length; i++) {
    if( Object.prototype.toString.call( arr[i] ) !== '[object Array]' ) {
       console.log(arr[i]);
}
Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
2

You can loop through the array and check whether each value is an array or not using JavaScript's instanceof operator.

var array = [],  // This is your array
    result = []; // This is the result array

// Loop through each index within our array
for (var i = 0; i < array.length; i++)
    /* If the value held at the current index ISN'T an array
     * add it to our result array. */
    if (!(array[i] instanceof Array))
        result.push(array[i]);

// Log the result array
console.log(result);

JSFiddle demo.

> ["Stories", "Tasks", "In Progress", "In Review", "Completed"] 
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • 1
    I liked your idea on using `instanceof`to check whether it is an array or not. While I was trying to learn about I came across this [blog](http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/) `instanceof` has a drawback for `Array`. Instead you can use as given in this [answer](http://stackoverflow.com/a/4775737/1671639) – Praveen Aug 20 '14 at 09:33
1

Very simple, with three lines you can filter the array:

// Arr is your Array :)
var result = arr.filter(function(value){
  return typeof value != 'array' && typeof value != 'object';
});

// It shows ["Stories", "Tasks", "In Progress", "In Review", "Completed"]
console.log(result); 

See the jsfiddle: http://jsfiddle.net/j4n99uw8/1/.

UPDATED: Also you can extends the Array and use in another sides:

Array.prototype.oneDimension = function(){
   return this.filter(function(value){
     return typeof value != 'array' && typeof value != 'object';
   });
};

// In each array you can use it:
console.log( arr.oneDimension() );
console.log( ['9',['9'],['2']].oneDimension() ); // Only contains '9'.

See this jsfiddle: http://jsfiddle.net/j4n99uw8/2/

SnakeDrak
  • 3,406
  • 4
  • 28
  • 41
0

In more modern browsers, this also works:

array.filter(function(item){
  return typeof(item) !== "object";
});
Biketire
  • 2,019
  • 1
  • 23
  • 41