2

I have a for loop in which I am getting all the values one by one but I need to form those values into one array.

Can any one let me know how to get form all the values into one array.

for (var i = 0; i < marray.length; i++) {
    mId = marray[i].id;
    var yourArray = [];
    yourArray.push(marray);
    console.log(marray);
}

Output getting from the above code is : ["0123"] and ["3456"]

But the expected output is ["0123","3456"]
Jon
  • 428,835
  • 81
  • 738
  • 806
user1853128
  • 1,102
  • 7
  • 19
  • 43

4 Answers4

6

You are creating a new yourArray for each loop iteration. Instead of doing that, create it just once before starting the loop:

var yourArray = [];
for (var i = 0; i < marray.length; i++) {
    mId = marray[i].id;
    yourArray.push(mId);
}

Note that I have changed the code to read yourArray.push(mId) because from the question it seems that's what you want -- not yourArray.push(marray).

A more compact way of doing the same is to use the array map function like this:

var yourArray = marray.map(function(row) { return row.id; });

This last version won't work out of the box in IE 8, so if you care about that you need to take appropriate measures.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • I tried using .map but it is not working. could you please help me out – user1853128 Jun 16 '14 at 09:32
  • @user1853128: If it doesn't work for you then you are making some error. Please post a new question explaining what you did, what you expected and what happened instead. People who are available will answer. – Jon Jun 16 '14 at 09:39
  • Thank you jon, inorder to break the statement once the loop is done should I use return false or break. – user1853128 Jun 16 '14 at 09:43
  • @user1853128: What does "break the statement" mean? Again, I can't tell what you are asking exactly because there is only a very vague description. Please use the appropriate terminology and ask proper full questions when you are unsure about something. – Jon Jun 16 '14 at 09:45
0

decalare variable in outside for loop..

  var yourArray = [];


     for (var i = 0; i < marray.length; i++) {
                    mId = marray[i].id;
                    yourArray.push(mid);

                }
 console.log(yourArray);
Balachandran
  • 9,567
  • 1
  • 16
  • 26
0

Initialise yourArray before the loop

Try this

var yourArray = [];
for (var i = 0; i < marray.length; i++) {
    mId = marray[i].id;
    yourArray.push(mId);
}
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
  • 1
    i didn't have downvoted, but using `for...in` for arrays isn't a good practice for sequential looping over arrays. You should use `for` in that situation. Please refer to [this](http://stackoverflow.com/a/500531/2412895) for answer. – KarelG Jun 16 '14 at 09:12
  • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. – PlasmaHH Jun 16 '14 at 09:23
0

The var yourArray is initialized to null each time you enter the loop. Define it outside loop.

Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
Amrita
  • 1
  • 3