最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to return array of 50 - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked Mar 27, 2018 at 9:03 Josh MillerJosh Miller 715 bronze badges 3
  • it should be i<50 or i<=49 – NoOorZ24 Commented Mar 27, 2018 at 9:05
  • 1 also you are pushing 3 different values each iteration instead of an object with 3 properties – Pete Commented Mar 27, 2018 at 9:05
  • stackoverflow.com/questions/3895478/… – georg Commented Mar 27, 2018 at 10:00
Add a comment  | 

9 Answers 9

Reset to default 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);

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)

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);

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;
}

   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);

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);

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)

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);

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.

发布评论

评论列表(0)

  1. 暂无评论