最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Iterator value undefined is not an entry object" Adding filter to map - Stack Overflow

programmeradmin2浏览0评论

I am trying to iterate through an array of objects to create a new Object that fits my plan ahead better. Everything works fine until I put a if sentence in the map and I have no clue why?

const TestArray = [{
    value: 0.2,
    Time: '1980',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.3,
    Time: '1983',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.2,
    Time: '1986',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.2,
    Time: '1988',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.2,
    Time: '2018',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  }
]
console.log("This works fine:")
console.log(Object.fromEntries(TestArray.map((item) => [item["Time"], item["value"]])))
console.log("but when adding filter it crashes:")
console.log(Object.fromEntries(TestArray.map((item) => {
  if (!(parseInt(item["validTo"]) > item["Time"])) {
    [item["Time"], item["value"]]
  }
})))

I am trying to iterate through an array of objects to create a new Object that fits my plan ahead better. Everything works fine until I put a if sentence in the map and I have no clue why?

const TestArray = [{
    value: 0.2,
    Time: '1980',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.3,
    Time: '1983',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.2,
    Time: '1986',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.2,
    Time: '1988',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  },
  {
    value: 0.2,
    Time: '2018',
    Region: 'Oklahoma',
    RegionNumber: '1620',
    validTo: 2017
  }
]
console.log("This works fine:")
console.log(Object.fromEntries(TestArray.map((item) => [item["Time"], item["value"]])))
console.log("but when adding filter it crashes:")
console.log(Object.fromEntries(TestArray.map((item) => {
  if (!(parseInt(item["validTo"]) > item["Time"])) {
    [item["Time"], item["value"]]
  }
})))

Share Improve this question edited Apr 20, 2022 at 1:08 Phil 165k25 gold badges259 silver badges267 bronze badges asked Apr 20, 2022 at 1:05 matievamatieva 3852 gold badges5 silver badges15 bronze badges 3
  • 2 You are not returning from your .map() callback. You should use .filter() followed by .map() if you want to remove elements and then map them. – Nick Parsons Commented Apr 20, 2022 at 1:08
  • @NickParsons Ah that worked thanks, Could you maybe link me to a site explaining why the if sentence makes the error happen as I don't really understand why still? But what you suggested fixed my problem. – matieva Commented Apr 20, 2022 at 1:10
  • 2 The .map() method won't ever remove elements, it will always return an array of the same length. So you must map each element from TestArray to another element by returning that from your callback. If you don't return anything, then you get undefined (so you end up with [undefined, undefined, ...] as the mapped array) that you then pass to Object.fromEntries(). This method doesn't expect to see undefined, it instead expects to see a [key, value] pair array, so your code crashes – Nick Parsons Commented Apr 20, 2022 at 1:23
Add a comment  | 

2 Answers 2

Reset to default 18

If anyone is ending up on this Question while working with the JavaScript Map object like me:

const map = new Map(["a", 1], ["b", 2]);    // Iterator value is not an entry object
const map = new Map([["a", 1], ["b", 2]]);  // Correct, just forgot the []

Please find the corrected code below

Object.fromEntries(TestArray.map((item) => {
  if (!(parseInt(item["validTo"]) > item["Time"])) {
    return [item["Time"], item["value"]]
  }
}).filter(item => item))

I have seen that you have already understood why your code did not work with the comments above.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论