admin管理员组文章数量:1392007
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
6 Answers
Reset to default 2You 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);
本文标签: javascriptHow to create array for loopStack Overflow
版权声明:本文标题:javascript - How to create array for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744763212a2623875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论