Why for loop returning last value alone instead of all other values in javascript

huangapple go评论110阅读模式
英文:

Why for loop returning last value alone instead of all other values in javascript

问题

以下是代码的翻译部分:

我有一个下面的函数

function test(){
    const obj = {}
    const arr = []

    for(let i = 0; i < 100; i++) {
        obj.i = i
        arr.push(obj)
    }

    return arr
} 

const res = test();
console.log(res);

这段代码返回了以下结果:

[{"i":99},{"i":99},{"i":99},...,{"i":99}]

但为什么它没有返回以下结果:

[{"i":0},{"i":1},...,{"i":99}]

有什么原因。

英文:

I have function something below,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function test(){
    const obj = {}
    const arr = []

    for(let i = 0; i &lt; 100; i++) {
        obj.i = i
        arr.push(obj)
    }

    return arr
} 

const res = test();
console.log(res);

<!-- end snippet -->

which returns

[{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99},{&quot;i&quot;:99}]

but why it not return

[{&quot;i&quot;:0},{&quot;i&quot;:1},...,{&quot;i&quot;:99}]

any reason.

答案1

得分: 2

你每次循环时都在替换该值。请像这样操作:

function test(){
     const arr = []
     for(let i = 0; i &lt; 100; i++) {
         const obj = {}
        obj.i = i
        arr.push(obj)
     }

    return arr
} 

const res = test();
console.log(res);
英文:

You are replacing the value each time it is looping.Do like this

function test(){
     const arr = []
     for(let i = 0; i &lt; 100; i++) {
         const obj = {}
        obj.i = i
        arr.push(obj)
     }

    return arr
} 

const res = test();
console.log(res);

huangapple
  • 本文由 发表于 2023年5月22日 18:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定