1

I'm trying to convert string to object with the below code:

wishlisted= [ '626601b89f1c16d0e42fd58e' ]
console.log(wishlisted.map(id => ({ id })));

Output for this came as:

[ { id: '626601b89f1c16d0e42fd58e' } ]

But from the output, I think id is of String type. I want to return it just like MongoDB's id, like: 626601b89f1c16d0e42fd58e, not as a string.

When I tried to check the type of id using:

console.log(typeof wishlisted[0].id);

it came as undefined. What could be the best way to do this type casting?

Bogota
  • 401
  • 4
  • 15
  • 2
    its undefined because you didn't store the value you just `console.log` it. You need to do `const x = wishlisted.map(id => ({ id }))` then try `console.log(typeof x[0].id)` – Usama May 09 '22 at 05:32

0 Answers0