-1

here is my code:

function employee(rex1){
    const rex = {...rex1}.map((key , value)=>{
        return `${key} : ${value}`


    })
    return `your result is ${rex.join(',')}`
}
console.log(employee({name : 'ahmed' , age : 20})

and it get error as below:

Uncaught TypeError: {(intermediate value)}.map is not a function

Zhubei Federer
  • 1,274
  • 2
  • 10
  • 27
mostafa Ahmed
  • 21
  • 1
  • 1
  • 1
  • 1
    Objects do not have a `map` method. Arrays do ~ [`Array.prototype.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) – Phil Sep 03 '19 at 00:48
  • Hey, welcome to SO. To make a good answer here, please be sure to include more detail about your question, state exactly what you need help with, and properly format your code. Thanks! – Jake Sep 03 '19 at 00:58

1 Answers1

2

If you want to iterate over the key / value pairs in an object, you can use Object.entries() to retrieve them as an array in the form

[[key1, val1], [key2, val2], ... [keyn, valn]]

For example

function employee(rex1) {
  const rex = Object.entries(rex1).map(([key , value]) => `${key} : ${value}`)

  return `your result is ${rex.join(',')}`
}
console.info(employee({name : 'ahmed' , age : 20}))
Phil
  • 157,677
  • 23
  • 242
  • 245