1

I am trying to return an array of object with 50 element inside of it

this what i am looking to achieve

  arrayofobject = [
    {
     id:0,
     Email: "Empty Email 0",
     Name: "Empty Name 0"
    },
    {
     id:1,
     Email: "Empty Email 1",
     Name: "Empty Name 1"
    }
............
 {
     id:49,
     Email: "Empty Email 49",
     Name: "Empty Name 49"
    }
]

I used the code below but still not able to achieve the result

var arrayofobject = []

for(var i = 0; i<=50; i++){
  arrayofobject.push("id" + i)
 arrayofobject.push("email"  + i)
 arrayofobject.push("name" + i)
}

i tried diffrent approch but i am still having a hard time to get the needed result

9 Answers9

8

This should work for you:

var objects = [];
var n = 0;
while( n < 50 ){
  objects.push({
    id: n,
    email: 'email' + n,
    name: 'name' + n,
  });
  n++;
}
console.log(objects);
Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
BRO_THOM
  • 824
  • 9
  • 24
  • 4
    You should usually use `for` instead of `while` when there is a known number of iterations like in this example. And even when you'd like to use `while` you could increase `n` directly in the condition like `while (n++ < 50) { ... ` – Benjamin Lüscher Mar 27 '18 at 09:24
  • This was the most simple, and readable example I could come up. OP isn't an expert so I wanted something that was readable and explained what it did just by looking at it. – BRO_THOM Mar 27 '18 at 09:26
  • 2
    OK I do understand your intend, but I would prefer the for-loop in this example (also for beginners) because it's best practice. – Benjamin Lüscher Mar 27 '18 at 09:34
6

var arrayOfObjects = [];
    
for (var i = 0; i < 50; i++) {
  var newObj = {
    id: i,
    email: "Empty email " + i,
    name: "Empty name " + i
  }

  arrayOfObjects.push(newObj);
}

console.log(arrayOfObjects);
Ben Thomas
  • 3,180
  • 2
  • 20
  • 38
Mick
  • 837
  • 8
  • 23
5

Use array.from its the best approach for that kind of situation

const result = Array.from({ length: 50 }, (x, z) => [
  { id: z }, { email: "Empty User " + z },{name: "Empy Name " + z}
]);

console.log(result)
Salman A
  • 262,204
  • 82
  • 430
  • 521
Abslen Char
  • 3,071
  • 3
  • 15
  • 29
3

   let arrayofobject=[];
   for(var i = 0; i<50; i++){
       let a = {};
       a.id = "id"+i;
       a.email = "email"+i;
       a.name = "name"+i;
       arrayofobject.push(a);
    }
    console.log(arrayofobject);
Deepu Reghunath
  • 8,132
  • 2
  • 38
  • 47
KoIIIeY
  • 533
  • 1
  • 7
  • 26
3

You're pushing individual values. What you want is an array of objects.

In ES5 and earlier:

var arrayofobject = [];
for(var i = 0; i < 50; i++){ // Note <, not <=, if you want 0-49
    arrayofobject.push({
        id: i,
        email: "Empty Email " + i,
        name: "Empty Name " + i
    });
}
console.log(arrayofobject);
.as-console-wrapper {
  max-height: 100% !important;
}

In ES2015+, you can use Array.from's callback (it can also be polyfilled):

let arrayofobject = Array.from({length: 50}, (_, i) => {
    return {
        id: i,
        email: "Empty Email " + i,
        name: "Empty Name " + i
    };
});
console.log(arrayofobject);
.as-console-wrapper {
  max-height: 100% !important;
}

or with a concise function body:

let arrayofobject = Array.from({length: 50}, (_, i) => ({
    id: i,
    email: "Empty Email " + i,
    name: "Empty Name " + i
}));
console.log(arrayofobject);
.as-console-wrapper {
  max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Your condition should be i<50 and not i<=50 if you want id till 49 only. Also, you need to create a object with those properties and push it to the arrayofobject array like below:

var arrayofobject = []

for(var i = 0; i<50; i++){
  var obj = {};
  obj["id"] = i;
  obj["Email"] = "Empty Email " + i;
  obj["Name"] = "Empty Name " + i;
  arrayofobject.push(obj);
}

console.log(arrayofobject);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
1

You are currently pushing 3 object by iteration.

arrayofobject.push("id" + i) //Add 1 element
arrayofobject.push("email"  + i)//Add 1 element
arrayofobject.push("name" + i)//Add 1 element

You should only push one object like below

var arrayofobject = []

for (var i = 0; i <= 49; i++) {
  arrayofobject.push({
    id: i,
    Email: "Empty Email " + i,
    Name: "Empty Name " + i
  });
}

console.log(arrayofobject)
Weedoze
  • 13,683
  • 1
  • 33
  • 63
1

var arrayList = [];
var uptoCount = 50;
for (var i = 0; i < uptoCount; i++) {
  var tempObj = {
    id: i,
    email: "Empty email " + i,
    name: "Empty name " + i
  }

  arrayList.push(tempObj);
}

console.log(arrayList);
mayur kasar
  • 165
  • 6
0

An alternate clean solution would be using array fill and map:

arr = Array(50).fill(0).map((val, index) => ({
     id: val + index,
     Email: "Empty Email " + (val + index),
     Name: "Empty Name " + (val + index)
    }));
console.log(arr);

But if you are making high performance JS application, and if you work with big/huge arrays, Array.map(..) creates big overload in both - memory and processor use, as it creates a copy of array.

Classic For loop is recommended as everyone else suggested.

Abdul Rafay
  • 3,241
  • 4
  • 25
  • 50