简体   繁体   English

地图返回对象的未定义数组?

[英]map return undefined array of object?

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 if语句产生了这样的内容

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

How to fix the undefined part? 如何修复未定义的部分? I want to skip the iteration. 我想跳过迭代。

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. 假设mapArray.prototype.map某种变体,则map产生从输入数组到输出数组的1:1值映射。

When you want to exclude values from your input array, use Array.prototype.filter : 如果要从输入数组中排除值,请使用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 本示例假定items.result是一个数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM