2
const newDate = map(items.result, (obj => {
  if (isDateWithinRage(obj.date_from)) {
    return {
      "date": obj.join_date,
      "name": obj.student.name
    }
  }
}))

The if statement has produced something like this

[Object, Object, Object, Object, Object, undefined, undefined, undefined, undefined, Object, Object, Object]

How to fix the undefined part? I want to skip the iteration.

1

1 Answer 1

3

Assuming map is some variant of Array.prototype.map, map produces a 1:1 mapping of values from your input array to an output array.

When you want to exclude values from your input array, use Array.prototype.filter:

const newDate =
  items
    .result
    .filter(obj => isDateWithinRange(obj.date_from))
    .map(obj => ({
      date: obj.join_date,
      name: obj.student.name
    }));

This example assumes that items.result is an Array

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.