Reputation: 2620
I have a dynamic object which will add new object and update existing array. I need to check below question
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
}
]
]
Upvotes: 0
Views: 61
Reputation: 100175
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
Upvotes: 0
Reputation: 2097
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 */
}
Upvotes: 0
Reputation: 25882
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.
Upvotes: 1