Jimmy
Jimmy

Reputation: 3860

no-return-assign when mapping over object values

I am trying to create an object whose keys and values are the keys of a different object. However I am getting a linting error:

ESLint: Arrow function should not return assignment.(no-return-assign)

enter image description here

const obj = {
  a: 1, b: 2, c: 3,
};
const options: {[key: string]: string} = {};
Object.keys(obj).map(i => options[i] = i);

Upvotes: 0

Views: 1197

Answers (1)

matt.h
matt.h

Reputation: 484

JS 101. When you use an arrow function without brackets, you should always put a returned value on the right side. If you want to run options[i] = i, you should put brackets around it and use forEach instead of map. map returns another array that contains all the returned values from the provided function inside.

Fix it as follows.

Object.keys(obj).forEach((i) => {
  options[i] = i;
});

However, since you say you want to create an object with keys and values from the values and keys of another object, you may use the following codes.

options = Object.keys(obj).reduce((prev, current) => {
  return {
    ...prev,
    current: obj[current],
  };
}, {});

Upvotes: 3

Related Questions