0

I want to get below result in the end.

[{x: '10', y: '5'}, {x: '8', y: '4'}]

I don't understand why the result get by the following code is

[{x: '8', y: '4'}, {x: '8', y: '4'}]

var test =[["10", "5"],["8","4"]];
var series = {};
var sample = [];
for (var i = 0 ; i < test.length ; i++){
  series.x = test[i][0];
  series.y = test[i][1];
  console.log(i);
  console.log(series);
  sample.push(series);
};
console.log(sample);

Can you give me some advice on what I lack?

6
  • 3
    You'll need to create a new object in each cycle of the loop. What you are currently doing is updating the same object in each loop. Your current results is an array of object references, all to the same object. Commented Jan 14, 2022 at 4:24
  • in the loop: let series = { x: test[i][0], y: test[i][1] }; Commented Jan 14, 2022 at 4:25
  • got it, sorry I made a careless mistake. Thanks!! Commented Jan 14, 2022 at 4:28
  • Also, consider the 1-liner: let sample = test.map(a => ({ x:a[0], y:a[1] })); Commented Jan 14, 2022 at 4:29
  • 1
    This should work let sample = test.map(([x,y])) => ({ x, y })); Commented Jan 14, 2022 at 5:01

6 Answers 6

0

Hello @shinobu you are doing a simple mistake you are not clearing the object inside object. I am adding a line to the code and it will solve it

var test =[["10", "5"],["8","4"]];
var series = {};
var sample = [];
for (var i = 0 ; i < test.length ; i++){
  series = {};
  series.x = test[i][0];
  series.y = test[i][1];
  sample.push(series);
};
console.log(sample);
Sign up to request clarification or add additional context in comments.

Comments

0

series is an object, what you have done in the loop basically change the value of members of series twice and push series into sample array twice. Since sample[0] and sample[1] point to the same object, they obviously have the same members, you can verify it with console.log(sample[0] === sample[1]).

Comments

0

Looks like you are pushing the references of the same object (i.e. series) into the array (i.e. sample) twice. That's why the two elements in the results list looks the same. You may want to make a little change (see below) to get what you want.

var test =[["10", "5"],["8","4"]];
var series; /* declare a new variable */
var sample = [];
for (var i = 0 ; i < test.length ; i++){
  series = {};  /* assign a new object */
  series.x = test[i][0];
  series.y = test[i][1];
  console.log(i);
  console.log(series);
  sample.push(series); /* push the reference of the new object for loop i */
};
console.log(sample);

Comments

0

By using reduce from ES6 array methods, you can remove most of the codes and replace it with the below code:

var test =[["10", "5"],["8","4"]];
var result = test.reduce((acc, current) => {
    acc.push({x: current[0], y: current[1]});
    return acc;
}, []);
console.log(result);

Let me know if you have any doubts in this ! Happy codiing

Comments

0

The cleanest way is to use map():

var test = [["10", "5"],["8","4"]];
var sample = test.map(([ x, y ]) => ({ x, y }));

console.log(sample);



Regarding your original code, as mentioned, you'll need to create the object inside the loop, so you alter the reference to the first one:

var test =[["10", "5"],["8","4"]];
var sample = [];

for (var i = 0 ; i < test.length ; i++){
  var series = {};
  series.x = test[i][0];
  series.y = test[i][1];
  sample.push(series);
};
console.log(sample);

Comments

0

You can use spread operator

var test = [["10", "5"], ["8", "4"]];
var series = {};
var sample = [];
for (var i = 0; i < test.length; i++) {
 series.x = test[i][0];
 series.y = test[i][1];
 sample.push({ ...series });
};
console.log(sample);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.