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?

1
  • 2
    what have you been doing curently? Commented Aug 20, 2014 at 9:14

4 Answers 4

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

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"] 

1 Comment

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
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/

Comments

0

In more modern browsers, this also works:

array.filter(function(item){
  return typeof(item) !== "object";
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.