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

javascript - How to create array for loop - Stack Overflow

programmeradmin0浏览0评论

I want to create a array like this:

[{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}]

How can I do this in Javascript?

I have tried this:

for(i = 0; i < 3; i++){
    var b = i;
    var c = i+1;
    var d = i+2;
};
dataResult={"b":b,"c":c,"d":d};

alert(dataResult)  //not working result [{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}] 

I want to create a array like this:

[{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}]

How can I do this in Javascript?

I have tried this:

for(i = 0; i < 3; i++){
    var b = i;
    var c = i+1;
    var d = i+2;
};
dataResult={"b":b,"c":c,"d":d};

alert(dataResult)  //not working result [{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}] 
Share Improve this question edited May 30, 2018 at 6:28 t.niese 40.9k9 gold badges78 silver badges109 bronze badges asked May 30, 2018 at 6:25 FierMan DearGodFierMan DearGod 111 silver badge1 bronze badge 1
  • 1 Writing the title of the question in UPPERCASE does not make it more important or more likely to be answered. It does rather the opposite. – t.niese Commented May 30, 2018 at 6:29
Add a ment  | 

6 Answers 6

Reset to default 2

You are just overriding value of 'b','c','d' and at the end assigning that value to 'dataResult', so you are not getting expected result.

Try this.

dataResult = [];
for(i = 0; i < 3; i++){
    dataResult.push({ 'b': i, 'c': i+1, 'd': i+2 });
};
console.log(dataResult);

You'll have to create the object inside the loop, and then push it to the array:

const arr = [];
for (let i = 0; i < 3; i++) {
  var b = i;
  var c = i + 1;
  var d = i + 2;
  arr.push({ b, c, d });
}
console.log(arr);

But it would be a bit more elegant to use Array.from here:

const arr = Array.from({ length: 3 }, (_, i) => {
  const b = i;
  const c = i + 1;
  const d = i + 2;
  return { b, c, d };
});
console.log(arr);

Create the object inside the loop and push it to an array

var arr = [];
for (var i = 0; i < 3; i++) {
  let obj = {
    b: i,
    c: i + 1,
    d: i + 2,
  }
  arr.push(obj)
};
console.log(arr)

var myArr = [];

for(var i = 0; i < 3; i++){
	var data = i;
  
	myArr.push({
     b: data,
     c: data + 1,
     d: data + 2
  })
}

console.log(myArr)

You were creating the object outside the loop. You need to create object inside the loop.

Try following

var arr = [];
for(let i = 0; i < 3; i++){
    var b = i;
    var c = b+1; // as b = i, you can change c = b + 1
    var d = c+1; // as c = i + 1, you can change d = c + 1
    arr.push({b,c,d});
};
console.log(arr);

You are setting the value of b, c, d after it loops so it puts the latest value of b, c, d in dataResult. Instead, you should initialize dataResult with an empty array and push values to the array after every step of the loop

var a,b,c;
var dataResult = [];
for(i = 0; i < 3; i++){
     b = i;
     c = i+1;
    d = i+2;
dataResult.push({"b":b, "c":c, "d":d});
};

alert(dataResult);
发布评论

评论列表(0)

  1. 暂无评论