0

I have a dynamic object which will add new object and update existing array. I need to check below question

  1. wheather array has another array?
  2. how to get data from array of array ?

below is the array object

[
"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
    }
]
]
4
  • Have you tried anything? Commented Aug 12, 2014 at 6:16
  • isArray functions commonly include Object.prototype.toString.call(obj) == '[object Array]'. Maybe that's a start to working out if a member is an array or not? Commented Aug 12, 2014 at 6:16
  • This looks like homework. Commented Aug 12, 2014 at 6:29
  • @JoeMoe1984 It is a problem which could be found in many projects, so it doesn't matter if it's a homework. Commented Aug 12, 2014 at 6:30

3 Answers 3

1

I'm addressing what I understood & what I know

I don't know if there are any inbuilt functions are there, you can iterate over array and get your answers.

Ques:- wheather array has another array?

You can use Array.some for this like bellow.

var arr = [1,2,3,[1,2,3],5]
arr.some(function(element){
     return Array.isArray(element)
 })// returns true

it will return true if any element will be array.

Ques:- how to get data from array of array ?

You can use forEach and check that if it is an array access the element

var arr = [1,2,3,[1,2,3],5];
arr.forEach(function(element){
      if(Array.isArray(element)){
         console.log(element[0])
       }
}); //prints 1

If it gives a way to start with it's fine.

NOTE:- Some & ForEach are not supported by IE8. There for loop will work.

0

As mentioned in the comments you can use isArray method

var arr=/* your array */
var pos=[];
var hasArray=false;
for(var i=0;i<arr.length;i++)
{
   if(Array.isArray(arr[i])){
      hasArray=true;
      pos.push(i);
    }
}
// now getting sub arrays
for(var i=0;i<pos.length;i++)
{
   var subArr=arr[pos[i]];
   /* do something with this subarr */
}
0

you could loop thru the array, check if the element is array as:

for( var i = 0, len = arr.length; i < len; i++) {
    //check if arr[i] is Array
    if(arr[i] instanceof Array) {
        //its array
        var objArr = arr[i];
        //loop thru all objects
        for(var x = 0, xlen= Object.keys(objArr).length; x < xlen; x++) {
            //loop thru object to get all values
            var obj = objArr[x];
            for(var k in obj) {
                console.log( "Key:"+ k + " value:" + obj[k] );
            }
        }
    }
}

Demo:: jsFiddle

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.