JavaScript |获取对象内的数据范围

时间:2019-03-09 05:06:47

标签: javascript ecmascript-6

我正在尝试编写一个函数,该函数创建一个新的对象数组,为我提供范围为fromto指定参数的所有数据。

const data = [
  {
    timestamp: '2015-09-01T16:00:00.000Z',
    temperature: 27.1,
    dewPoint: 17.1,
  },
  {
    timestamp: '2015-09-01T16:10:00.000Z',
    temperature: 27.2,
    dewPoint: 17.2,
  },
  {
    timestamp: '2015-09-01T16:20:00.000Z',
    temperature: 27.3,
    dewPoint: 17.3,
  },
  {
    timestamp: '2015-09-01T16:30:00.000Z',
    temperature: 27.4,
    dewPoint: 17.4,
  }
]

这个想法是要有一个辅助方法来过滤并输出fromto中的数据到新数组[]

function getValuesInRange(from, to) {
  let sortedArray = []
  let filteredValues = []
  let index = indexOf(from) // error => indexOf is not defined

  while (index < sortedArray.length) {
    const valueAtIndex = sortedArray[index]
    if (valueAtIndex.key >= from && valueAtIndex.key < to) {
      filteredValues.push(valueAtIndex.value)
      index++
    } else {
      break
    }
  }
  console.log(filteredValues)
  return filteredValues
}

const fromDateTime = data[0].timestamp)
const toDateTime = data[2].timestamp)

getValuesInRange(fromDateTime, toDateTime)
  • 上述方法当前错误为indexOf is not defined

  • getValuesInRange()上,我是否在此相应地按摩数据以产生所需的结果?您还可以向我展示与该工作相当的ES6吗?

2 个答案:

答案 0 :(得分:2)

您可以使用.getTime()filter()

尝试以下方式
  

getTime() 方法返回自Unix Epoch

以来的毫秒数。

const data = [
  {
    timestamp: '2015-09-01T16:00:00.000Z',
    temperature: 27.1,
    dewPoint: 17.1,
  },
  {
    timestamp: '2015-09-01T16:10:00.000Z',
    temperature: 27.2,
    dewPoint: 17.2,
  },
  {
    timestamp: '2015-09-01T16:20:00.000Z',
    temperature: 27.3,
    dewPoint: 17.3,
  },
  {
    timestamp: '2015-09-01T16:30:00.000Z',
    temperature: 27.4,
    dewPoint: 17.4,
  }
]

function getData(arr,from,to){
  from = new Date(from).getTime();
  to = new Date(to).getTime();
  return arr.filter(obj => {
    let ms = new Date(obj.timestamp).getTime();
    return ms < to && ms > from
  })
}
console.log(getData(data,'2015-09-01T16:00:00.000Z','2015-09-01T16:30:00.000Z'))

答案 1 :(得分:1)

indexOf()是数组对象的方法。不能单独使用。它必须与数组一起使用

let index = arrayname.indexOf(from) 

因为您不清楚上面代码行中的问题,所以将arrayname替换为必须在其中找到 from

的索引的数组

const data = [
  {
    timestamp: '2015-09-01T16:00:00.000Z',
    temperature: 27.1,
    dewPoint: 17.1,
  },
  {
    timestamp: '2015-09-01T16:10:00.000Z',
    temperature: 27.2,
    dewPoint: 17.2,
  },
  {
    timestamp: '2015-09-01T16:20:00.000Z',
    temperature: 27.3,
    dewPoint: 17.3,
  },
  {
    timestamp: '2015-09-01T16:30:00.000Z',
    temperature: 27.4,
    dewPoint: 17.4,
  }
]


function getValuesInRange(from, to) {
  let sortedArray = []
  let filteredValues = []
  let index = arrayname.indexOf(from) // error => indexOf is not defined

  while (index < sortedArray.length) {
    const valueAtIndex = sortedArray[index]
    if (valueAtIndex.key >= from && valueAtIndex.key < to) {
      filteredValues.push(valueAtIndex.value)
      index++
    } else {
      break
    }
  }
  console.log(filteredValues)
  return filteredValues
}

const fromDateTime = data[0].timestamp
const toDateTime = data[2].timestamp
getValuesInRange(fromDateTime, toDateTime)