score:1

Accepted answer

To access object values knowing key name is enough, You need to use proper key names to access values

change to this

xydata = data.map(item => ({x: item.IG, y: item.FLFPR}));

score:1

You can use [] to access object properties dynamically.

let x = {dog: 3, cat: 4, key: 5};
let key = 'dog';

console.log(x.key); // 5
console.log(x['key']); // also 5
console.log(x[key]); // 3

In your case:

xydata = data.map(item => ({x: item[inputx], y: item[inputy]}));

Similarly, you can use [] when creating objects with dynamic property names.

let key = 'dog';
let x = {
  cat: 5,
  [key]: 6,
  key: 7,
};

console.log(x.key); // 7
console.log(x.dog); // 6


Related Query

More Query from same tag