Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
2  
what have you been doing curently? –  Mritunjay Aug 20 at 9:14

4 Answers 4

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]);
}
share|improve this answer

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"] 
share|improve this answer
1  
I liked your idea on using instanceofto check whether it is an array or not. While I was trying to learn about I came across this blog instanceof has a drawback for Array. Instead you can use as given in this answer –  Praveen Aug 20 at 9:33

In more modern browsers, this also works:

array.filter(function(item){
  return typeof(item) !== "object";
});
share|improve this answer

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/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.