An object is converted to an array object

expectation:

categories:{0: "", 1: "", 2: "", 3: "", 4: ""}



catlist= [{cat: ""}{cat: ""}{cat: ""}{cat: ""}{cat: ""}]

actual

for(let i in categories ){
    catag.cat = categories[i];
    catlist.push(catag);
    console.log(catlist);             
}
catlist= [{cat: ""}{cat: ""}{cat: ""}{cat: ""}{cat: ""}]

I really don"t know why the circular array push changes the value of the previous push

Mar.15,2021

because your catag is not redefined, it all points to the same reference, so you have to redefine one at a time in the loop, or directly push

.
for (let i in categories) {
  catlist.push({cat: categories[i]})
}

simple, because the address of your catag has not changed, you change the value of the same catag each time, and then insert the same catag into the array.
, rather than the array push you guessed, will change the value of the previous push.
change it to this:

for(let i in categories ){
    const catag={}
    catag.cat = categories[i];
    catlist.push(catag);
    console.log(catlist);             
}

let categories= {0: "language", 1: "style", 2: "scene", 3: "emotion", 4: "theme"}, catlist= []; Object.values (categories). Map ((item) = > {catlist.push ({cat:item})})


catag is an object, and the mutual assignment of objects in JS is a reference to the value, so each modified item is the value of the same item, resulting in the previous overwrite.

var a = {aa: 123};
var b = a;

b.aa = 456;
a // {aa: 456}

you can make a shallow copy


first.

var categories = {0: "language", 1: "style", 2: "scene", 3: "emotion", 4: "theme"};

var catlist = [];
for (let i in categories) {

var catag = {};
catag.cat = categories[i];
console.log(catag.cat);
catlist.push(catag);
console.log(catlist);             

}


Ah, if you write catag like this, at first I can see that one is built into the system, but later it turns out that it is not. This kind of problem is often encountered in js, why should it not be so logically after I have changed it, and why it has been changed once and has been changed before? The key lies in the characteristics of js, which I don't quite understand, but I just need to know why. Because you put catag into catlist through push, you don't put the value of catag, but catag, so every time you change catag, you don't redefine it. So at the end of the day, you can look at it this way: `catag = ['cat':' education'];
catlist = [catag,catag] `
is the reason. There are several solutions, and a direct push corresponds to the parameter result. Or, as you did upstairs, redefine the catag variable each time.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e47cde-44c6e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e47cde-44c6e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?