0

I have some problem with my code when I create an array in this loop its repeat same as previous and not correct

getTimeShiftPermission = (data) => {
const toDay = moment().format('[today] dddd')
let newData = []
if (data) {
  let newItem = {}
  data.map((i, index) => {
    if (i.start !== '00:00' || i.end !== '23:59') {
      console.log('index', index, i)
      newItem.start = i.start
      newItem.end = i.end
      newItem.day = moment.weekdays(index)
      newData.push(newItem)
    } else if (i.start === "" || i.end === "") {
      return []
    }
  })
  console.log('newData', newData)
  return newData
} else {
  return []
}}

and its result

0: {start: "03:02", end: "14:51", day: "Thursday"} 1: {start: "03:02", end: "14:51", day: "Thursday"}

2
  • 1
    Please don't use .map only for iteration. Mapping returns an array already, you don't have to manually build it. Use .forEach or an actual loop for iteration. Commented Mar 13, 2020 at 20:21
  • What is the input and what is the expected output? Commented Mar 13, 2020 at 20:28

2 Answers 2

1

newItem is getting reused. This means every time you change in within the loop you are overwriting every one in the array. So at the end of the loop all of your objects will be equal to the last iteration.

You could just create a new object when you push it to the array:

newData.push({...newItem})

Or you could move the declaration inside the loop:

let newData = []
if (data) {
  data.map((i, index) => {
    if (i.start !== '00:00' || i.end !== '23:59') {
      let newItem = {} // Move it here
      newItem.start = i.start
      newItem.end = i.end
      newItem.day = moment.weekdays(index)
      newData.push(newItem)
    }
  })
  return newData
}

Or you could fix your map to actually utilize it how it is intended. For this you should use both filter and map:

if (data) {
  // Return the array directly
  return data.filter((i => {
    // If your condition passes, we will keep it
    if (i.start !== '00:00' || i.end !== '23:59') {
      return true;
    }
    // Else filter it out
    return false
  }).map((i, index) => ({ // Return a new object with your formatted values
    start: i.start  
    end: i.end
    day: moment.weekdays(index)
  })
}
Sign up to request clarification or add additional context in comments.

Comments

0

Please try this code.

getTimeShiftPermission = (data) => {
const toDay = moment().format('[today] dddd')
let newData = []
if (data) {
  newData = data.map((i, index) => {
    if (i.start !== '00:00' || i.end !== '23:59') {
      newItem.start = i.start
      newItem.end = i.end
      newItem.day = moment.weekdays(index)
      return {
          start: i.start,
          end: i.end,
          day: moment.weekdays(index)
      };
    }else{
       return null;
    }
  })

  newData = newData.Filter(x=>x!=null);
  console.log('newData', newData)
  return newData
} else {
  return []
}}

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.