I’m trying to push different objects at each time, but it changes the front components while looping the for loop. Here is my code.
JavaScript
x
36
36
1
let responseArray = [];
2
3
const sendBatchRequest = async function (response) {
4
try {
5
console.log(`response: ${JSON.stringify(response)}`);
6
if (responseArray.length < 9) {
7
responseArray.push(response); // push 9 components
8
console.log(`responseArray at if: ${JSON.stringify(responseArray)}`);
9
} else {
10
responseArray.push(response); // push last 10th component
11
console.log(`responseArray at else: ${JSON.stringify(responseArray)}`);
12
for (let i = 0; i < responseArray.length; i++) {
13
console.log(`responseArray ${i} - ${JSON.stringify(responseArray[i])}`);
14
}
15
// do something...
16
responseArray = []; // reset the array
17
}
18
} catch (err) {
19
console.log(`sendBatchRequest err: ${err}`);
20
}
21
}
22
23
const main = async function () {
24
try {
25
const result = {};
26
for (let i = 0; i < 5; i++) {
27
result.data = i;
28
await sendBatchRequest(result);
29
}
30
} catch (err) {
31
console.log(`main err: ${err}`);
32
}
33
}
34
35
main();
36
And my result looks like this.
JavaScript
1
11
11
1
response: {"data":0}
2
responseArray at if: [{"data":0}]
3
response: {"data":1}
4
responseArray at if: [{"data":1},{"data":1}]
5
response: {"data":2}
6
responseArray at if: [{"data":2},{"data":2},{"data":2}]
7
response: {"data":3}
8
responseArray at if: [{"data":3},{"data":3},{"data":3},{"data":3}]
9
response: {"data":4}
10
responseArray at if: [{"data":4},{"data":4},{"data":4},{"data":4},{"data":4}]
11
I can’t understand why at 2th loop, responseArray[0]
changes {"data":0}
to {"data":1}
. I want my responseArray
looks like [{"data":0},{"data":1"},...]
. What’s happening on Array.push(object)
at javascript? And how could I get the result what I want?
Advertisement
Answer
It’s because you’re passing the result variable as a reference, everytime the result.data changes, so as the pushed items on the responseArray
will be changed also.
So to avoid that. do this instead:
JavaScript
1
34
34
1
let responseArray = [];
2
3
const sendBatchRequest = async function (response) {
4
try {
5
console.log(`response: ${JSON.stringify(response)}`);
6
if (responseArray.length < 9) {
7
responseArray.push(response); // push 9 components
8
console.log(`responseArray at if: ${JSON.stringify(responseArray)}`);
9
} else {
10
responseArray.push(response); // push last 10th component
11
console.log(`responseArray at else: ${JSON.stringify(responseArray)}`);
12
for (let i = 0; i < responseArray.length; i++) {
13
console.log(`responseArray ${i} - ${JSON.stringify(responseArray[i])}`);
14
}
15
// do something...
16
responseArray = []; // reset the array
17
}
18
} catch (err) {
19
console.log(`sendBatchRequest err: ${err}`);
20
}
21
}
22
23
const main = async function () {
24
try {
25
for (let i = 0; i < 5; i++) {
26
const result = {data: i}; // Do this change
27
await sendBatchRequest(result);
28
}
29
} catch (err) {
30
console.log(`main err: ${err}`);
31
}
32
}
33
34
main();