0

I am trying to refactor a ReactJS component by using a map function. Is it possible to use the map() function to update multiple objects, or the same object twice? I am getting a syntax error when trying to update the initData object as follows:

days.map(day => {
      return (
        initData[`${day}From`] = myStr.slice(0, 2) 
        initData[`${day}To`] = myStr.slice(5, 7)
      );
    });

Note that I need to use return() due to React. Note that this works with one object:

days.map(day => {
      return (
        initData[`${day}From`] = myStr.slice(0, 2) 
      );
    });
3
  • 1
    initData where you have defined ? Commented Apr 18, 2018 at 10:02
  • It seems checking the code that you're using the wrong ' charachter. Have you tried to fix it? Commented Apr 18, 2018 at 10:04
  • 2
    Why do you need to return() due to react? Commented Apr 18, 2018 at 10:04

1 Answer 1

3

You're not updating day itself and not modifying the days array, so there's no point using map. Just use a for or forEach loop.

days.forEach(day => {
        initData[`${day}From`] = myStr.slice(0, 2) 
        initData[`${day}To`] = myStr.slice(5, 7)
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.