An example, I declared an object json
and in a for loop, I change the json's value and push it into the list2, But It didn't output the value what I expected.
var json = {
value: '',
text: '',
};
let list = [
{
a: 1,
b: 2,
c: 3
},
{
a: 4,
b: 5,
c: 6
}
];
let list2 = [];
for(i = 0; i < list.length; i++){
json.value = list[i].a;
json.text = list[i].b;
list2.push(json);
}
console.log(list2)
the expected output is
list2 = [
{
value: 1,
text: 2,
},
{
value: 4,
text: 5,
}
];
but the fact is:
list2 = [
{
value: 4,
text: 5,
},
{
value: 4,
text: 5,
}
];
Why?
PS:I know how to avoid this question, But I can't understand why it happened