Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数将记录值,但返回未定义的值_Javascript_Arrays_For Loop_Return_Undefined - Fatal编程技术网

Javascript 函数将记录值,但返回未定义的值

Javascript 函数将记录值,但返回未定义的值,javascript,arrays,for-loop,return,undefined,Javascript,Arrays,For Loop,Return,Undefined,下面的函数将记录newData的值,但调用时返回undefined。有人知道为什么会这样吗?此外,如果您对函数本身有任何反馈,我们将不胜感激 export const filterByDateTimeRange = (data=[{}], timeKey=[], startTime=moment(), stopTime=moment()) => { let newData = []; let i = 0; for(const item of data) { let ti

下面的函数将记录
newData
的值,但调用时返回
undefined
。有人知道为什么会这样吗?此外,如果您对函数本身有任何反馈,我们将不胜感激

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);
        }
      }
    })
  }
}

这个返回在map函数中。不返回filterByDateTimeRange()。如果要返回新数据。用for循环替换map函数


Map:

此返回在Map函数中。不返回filterByDateTimeRange()。如果要返回新数据。用for循环替换map函数


映射:

映射功能通常用于转换集合和存储结果,例如:

var squares = [2, 3, 4].map(x => { return x * x });
// result is squares = [4, 9, 16]
forEach
函数更适合在这里使用,因为您只想在数组上循环,而不关心存储转换

然后,当外部循环完成时,您的函数可以返回
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
}

map
函数通常用于转换集合并存储结果,例如:

var squares = [2, 3, 4].map(x => { return x * x });
// result is squares = [4, 9, 16]
forEach
函数更适合在这里使用,因为您只想在数组上循环,而不关心存储转换

然后,当外部循环完成时,您的函数可以返回
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
}

实际上,您从未返回新数据。你映射了它,但从不返回它。请注意,在映射方法中返回不算数。Map返回一个新数组,其中返回的值将取代原始数组的成员。但是如上所述,您从未对它做过任何事情在最后一个大括号之前,我非常确定您的代码将按照您的预期工作。尽管如此,当您从未实际使用结果时,我不会使用
.map()
;我会使用
.forEach()
来实现这一点。实际上,您永远不会返回新数据。你映射了它,但从不返回它。请注意,在映射方法中返回不算数。Map返回一个新数组,其中返回的值将取代原始数组的成员。但是如上所述,您从未对它做过任何事情在最后一个大括号之前,我非常确定您的代码将按照您的预期工作。尽管如此,当您从未实际使用结果时,我不会使用
.map()
;我会使用
.forEach()
来实现这一点。