An object problem in push

var text_2=["a","b","c","d","e","f"];
        var newarr=[];
        for(var i=0;i<text_2.length;iPP){
                newarr.push({i:text_2[i]});
        }
         console.log(newarr);

Why is the object key from push I: "a"?
clipboard.png
instead of 123456

Nov.02,2021

 var text_2=['a','b','c','d','e','f'];
    var newarr=[];
    for(var i=0;i<text_2.length;iPP){
        newarr.push({[i]:text_2[i]});
    }
     console.log(newarr);
     

clipboard.png


if you write I on key, he will default to the key name I, not the variable I
to {[I]: I}


Why use [] to frame I? Because [the name of the attribute] and the use. The name of the setting property is the same. It's just that the specific scene used is different, so the way to write it is different

look at the code:

var text_2=['a','b','c','d','e','f'];
var newarr=[];
for(var i=0;i<text_2.length;iPP){
    // text_2[i] => a,b,c...
    // i => 0,1,2,3,4...
    newarr.push({[i]: text_2[i]});
}
console.log(newarr);
Menu