0

I have below code

var obj = [];
var statuses = [];
var time ='';

statuses array has some values like 
statuses[0] = [1]
statuses[1] = [0]
statuses[2] = [1]
statuses[3] = [1]

Now i want to push these values in obj array such that when i do obj[0].length it should come to 5

If i do obj.push(([time, statuses])) it gives length as 2

When i do obj.push(([time, statuses[0],statuses[1],statuses[2],statuses[3]])); then i am getting value of obj[0].length as 5

I dont want to hardcode the values like statuses[0],[1] in obj.push as i am not sure how many values will be present in statuses array in future.

Ofcourse my status array will grow depending upon the values i receive from backend. So my obj[0].length should be number of objects in status array + time obj. Eg: if statuses has 8 values then my final obj[0].length should come to 9

I have tried using concat function but their is no luck.

Could anyone please help me out with how can I achieve this dynamically?

5
  • You can use a loop w3schools.com/js/js_loop_for.asp Commented Jul 2, 2015 at 9:35
  • No. For loop doesnt work for the way i want to push the values in obj. I tried with it also, but it doesnt solve my purpose. Commented Jul 2, 2015 at 9:36
  • So you want to push elements in obj array in following order: time, statuses[0], statuses[1], ... ? Right? Commented Jul 2, 2015 at 9:39
  • yes, but these objects should be in obj[0] instance and obj[0].length should equal these values Commented Jul 2, 2015 at 9:41
  • If i do obj.push(([time, statuses])) it gives length as 2 , this is because the status itself is a array , hence when you refer to obj it will be like this obj [ time, status [ 1, 2, 3, ] ] Commented Jul 2, 2015 at 9:52

2 Answers 2

1

Try this:

var temp = [];
temp.push(time);
foreach(function(s, i, statuses) {
    temp.push(s);
});
obj.push(temp);
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you correctly you should push your elements to first element of obj array

var obj = [[]];
var statuses = [];
var time ='';

statuses array has some values like 
statuses[0] = [1];
statuses[1] = [0];
statuses[2] = [1];
statuses[3] = [1];

// === SOLUTION ===

obj[0].push(time);
for(var i; i<statuses.length;i++)
{
   obj[0].push(statuses[i]);
}

EDIT: Answer to another question in comment Always try to draw and visualize how your arrays could look like when you populate its in your requested way.

var obj = [];
var statuses = [];
var time ='';

for(var k=0;k<12;k++)
{
    statuses[0] = [1];
    statuses[1] = [0];
    statuses[2] = [1];
    statuses[3] = [1];

    // === SOLUTION ===

    obj.push([]);
    obj[k].push(time);
    for(var i; i<statuses.length;i++)
    {
       obj[k].push(statuses[i]);
    }
}

1 Comment

This is partially correct, but i am pushing all these values in obj array from a parent for loop. So obj objects instances will depend upon the resultobj length of the object. Now i almost got what i wanted, but when the parent loop runs twice obj[0].length becomes 10 which i dont want.

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.