0

The function below will log the value of newData but returns undefined when called. Does anyone know why this might be? Also, any feedback on the function itself would be greatly appreciated!

export const filterByDateTimeRange = (data=[{}], timeKey=[], startTime=moment(), stopTime=moment()) => {
  let newData = [];
  let i = 0;
  for(const item of data) {
    let time;
    let index = 0
    timeKey.map(key => {
      time ? time = time[key] : time = item[key];
      index++
      if(index === timeKey.length) {
        if(moment(time).isBetween(startTime, stopTime, undefined, '[)')) {
          newData.push(item)
        };
        i++;
        if(i === data.length) {
          console.log(newData);
          return (newData);
        }
      }
    })
  }
}
2
  • 1
    You never actually return newData. You map it, but never return it. Note that returning in the mapping method doesn't count. Map returns a new array with the values you return within it taking the place of the original array's members. But as stated, you never do anything with it. Commented Aug 3, 2020 at 3:14
  • 1
    If you just added return newData; before the last brace, I'm pretty sure your code would work as you intend it to. That said though, I wouldn't use .map() when you never actually use the result; I'd use .forEach() for that. Commented Aug 3, 2020 at 3:20

2 Answers 2

2

The map function is usually used to transform a collection and store the results, for example:

var squares = [2, 3, 4].map(x => { return x * x });
// result is squares = [4, 9, 16]

The forEach function is more appropriate to use here since you just want to loop over the array and don't care about storing a transformation.

Then when the outer loop finishes your function can return newData

export const filterByDateTimeRange = (data=[{}], timeKey=[], startTime=moment(), stopTime=moment()) => {
  let newData = [];
  let i = 0;
  for(const item of data) {
    let time;
    let index = 0
    timeKey.forEach(key => {                         //changed to a forEach loop
      time ? time = time[key] : time = item[key];
      index++
      if(index === timeKey.length) {
        if(moment(time).isBetween(startTime, stopTime, undefined, '[)')) {
          newData.push(item)
        };
        i++;
        if(i === data.length) {
          console.log(newData);
        }
      }
    });
  }
  return newData; //add the return after your loop finishes
}
Sign up to request clarification or add additional context in comments.

Comments

0

This return inside a map function. Not return of filterByDateTimeRange(). If you want to return newData. Replace map function by for loop.

Map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

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.