-1

I am getting a strange error, could somebody help me fix this out?

const myArray = new Array(4)
    .keys()
    .map((v, i) => (
          console.log('print:', v, i)
      )
    )
console.log('myArray print: ', myArray);

I am getting this error:

TypeError: (intermediate value).keys(...).map is not a function

update:

i want to use the i to look up some stuff from another existing array.

const myArray = new Array(4)
    .keys()
    .map((v, i) => ({
      name: i
      })
    )
console.log('myArray print: ', myArray);

edit:

this question is not a duplicate, It is trying to iterate over a certain number and create a new object using the iterations.

moaningalways
  • 461
  • 1
  • 10
  • 21
  • the function keys when applied to the array returns an object, and the map function is not available for objects. – Daksh M. Dec 06 '17 at 10:13
  • What do you want to do with the code above? – Faly Dec 06 '17 at 10:13
  • Same issue and solution as the dupe target, except that `keys` is an iterator object instead of a HTML collection. – Cerbrus Dec 06 '17 at 10:14
  • You can use `entries()` instead of `map()` – Alexis Dec 06 '17 at 10:15
  • @Faly please see the update – moaningalways Dec 06 '17 at 10:17
  • @Alexis can you give an example? – moaningalways Dec 06 '17 at 10:17
  • Reopened question since it is not a duplicate of the linked question. – Derek 朕會功夫 Dec 06 '17 at 10:22
  • @Derek朕會功夫: How is it not a duplicate? It's the ___same issue___: Array-like return value that isn't an array. It's got the ___same solution___. – Cerbrus Dec 06 '17 at 10:33
  • @Cerbrus Not really. An iterator from an array with unindexed `undefine`s is fundamentally different than a live list. They might both yield the same error message, but they are different issues and require different answers. – Derek 朕會功夫 Dec 06 '17 at 10:35
  • @Derek朕會功夫: `Array.from(new Array(4).keys())` or `[...new Array(4),keys()]` works just fine. (From the dupe target) – Cerbrus Dec 06 '17 at 10:40
  • @Cerbrus Different answers as in different explanation. It just so happens that `Array.from` accepts both an iterator and a live list because they are both array-like. A rule of thumb is that a question is a duplicate if you can copy and paste an answer from the linked question without having to modify and it remains a valid answer. – Derek 朕會功夫 Dec 06 '17 at 10:43
  • And that's exactly my point. It's about converting array-like objects to the arrays a users expected them to be in the first place. It may not be the best dupe target, but I'm sure there's one out there. – Cerbrus Dec 06 '17 at 10:45
  • "they are both array-like" uhm no. Please don't say array or even something in that direction to an iterator. It is a different thing. The close-dupe here is not justified imo. – KarelG Dec 06 '17 at 10:47

2 Answers2

4

Array.keys() returns an iterator Object which is an object and does not have a property map in it. You can convert it to do

const myArray = [...new Array(4).keys()].map((v, i) => ({
      name: i
      })
    )
console.log('myArray print: ', myArray);
marvel308
  • 10,288
  • 1
  • 21
  • 32
3

You can use Array.from to get the expected output:

var myArray = Array.from(Array(4), (value, index) => ({ name: index}));
console.log('myArray print: ', myArray);
Faly
  • 13,291
  • 2
  • 19
  • 37