0

i have that lint error: Assignment to property of function parameter 'item'

What is the correct way to remove this error?

const resp = await getData(payload)
resp.forEach((item) => {
      item[0] = moment(item[0]).format('DD/MMM/YY');
});
Giuseppe Fantoni
  • 183
  • 4
  • 11
  • does this help? https://stackoverflow.com/questions/35637770/how-to-avoid-no-param-reassign-when-setting-a-property-on-a-dom-object – Alan Yu Nov 16 '21 at 13:44
  • 2
    You _are_ assigning to a property of the parameter. You could use `.map` and create a new array of new arrays, or disable the rule for this specific functionality. – jonrsharpe Nov 16 '21 at 13:46
  • 2
    Does `getData` return JSON? Where do you parse that JSON? Also, what's the structure of each `item`? Can you actually set the index like that? You should probably be using [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) rather than `forEach` for this operation. – Andy Nov 16 '21 at 13:47
  • [ [ "07/10/2020", 0.04, 0.05, 0.04 ], [ "07/10/2020", 0.04, 0.06, 0.04 ] ] @Andy thats the return of getData – Giuseppe Fantoni Nov 16 '21 at 14:04
  • I try to use the map but I can't use it well – Giuseppe Fantoni Nov 16 '21 at 14:05

1 Answers1

1

Here's an example with map.

const response = [ [ "07/10/2020", 0.04, 0.05, 0.04 ], [ "07/10/2020", 0.04, 0.06, 0.04 ] ];

// Iterate over the response
const data = response.map(item => {

  // For each item destrucuture the data from
  // the rest of the elements
  const [date, ...rest] = item;

  // Return a new array which has the updated
  // date, and the other elements then spread out
  return [
    moment(new Date(date)).format('DD/MMM/YY'),
    ...rest
  ];

});

console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Additional documentation

Andy
  • 61,948
  • 13
  • 68
  • 95