admin管理员组

文章数量:1356017

I have an array of ObjectIds like this:

[ 5cd295fbc53f626fd9d70894,
  5cb0624945111f1c5fdf2527,
  5cc290f5b90fc527c1e46efd,
  5cd6737f294e7b6ec3d9420f,
  5cd295fbc53f626fd9d70894,
  5cb0624945111f1c5fdf2527,
  5cc290f5b90fc527c1e46efd,
  5cd6737f294e7b6ec3d9420f,
  5cd295fbc53f626fd9d70894 ]

I need to convert each Objectid into strings.

I have tried toString() and JSON.stringify(). But toString() splits every character into a string and JSON.stringify() does not affect the array.

Actually I was working on finding the unique Ids in the array. I have tried many functions to get unique elements, but they seem to work only with numbers and strings and not ObjectIds.

On finding the type of element, it displayed it as an object.

So I'm trying to convert the array of objects to array of string, so that I can easily use one of the functions to get the unique elements.

Expected output would be like this:

[ "5cd295fbc53f626fd9d70894",
  "5cb0624945111f1c5fdf2527",
  "5cc290f5b90fc527c1e46efd",
  "5cd6737f294e7b6ec3d9420f",
  "5cd295fbc53f626fd9d70894",
  "5cb0624945111f1c5fdf2527",
  "5cc290f5b90fc527c1e46efd",
  "5cd6737f294e7b6ec3d9420f",
  "5cd295fbc53f626fd9d70894" ]

I have an array of ObjectIds like this:

[ 5cd295fbc53f626fd9d70894,
  5cb0624945111f1c5fdf2527,
  5cc290f5b90fc527c1e46efd,
  5cd6737f294e7b6ec3d9420f,
  5cd295fbc53f626fd9d70894,
  5cb0624945111f1c5fdf2527,
  5cc290f5b90fc527c1e46efd,
  5cd6737f294e7b6ec3d9420f,
  5cd295fbc53f626fd9d70894 ]

I need to convert each Objectid into strings.

I have tried toString() and JSON.stringify(). But toString() splits every character into a string and JSON.stringify() does not affect the array.

Actually I was working on finding the unique Ids in the array. I have tried many functions to get unique elements, but they seem to work only with numbers and strings and not ObjectIds.

On finding the type of element, it displayed it as an object.

So I'm trying to convert the array of objects to array of string, so that I can easily use one of the functions to get the unique elements.

Expected output would be like this:

[ "5cd295fbc53f626fd9d70894",
  "5cb0624945111f1c5fdf2527",
  "5cc290f5b90fc527c1e46efd",
  "5cd6737f294e7b6ec3d9420f",
  "5cd295fbc53f626fd9d70894",
  "5cb0624945111f1c5fdf2527",
  "5cc290f5b90fc527c1e46efd",
  "5cd6737f294e7b6ec3d9420f",
  "5cd295fbc53f626fd9d70894" ]
Share Improve this question edited Jun 13, 2019 at 13:04 shaochuancs 16.3k3 gold badges62 silver badges67 bronze badges asked Jun 13, 2019 at 12:56 Shambhavi_RBShambhavi_RB 1211 silver badge8 bronze badges 1
  • can you please include the code? – Abhishek Anand Commented Jun 13, 2019 at 13:10
Add a ment  | 

3 Answers 3

Reset to default 5

You could do it with map. Call toString for each element in the array and returning them in a new array as strings.

For example:

const stringsArray = objectIds.map(x => x.toString());

Then the most efficient way to get unique results in an array would be to put them on a Set. So it would look like the following:

const uniqueStrings = [...new Set(stringsArray)];

You can use map and pass the String constructor as a function, which will convert each Objectid into a string:

Objectids.map(String) //=> [ "5cd295fbc53f626fd9d70894",
                      //     "5cb0624945111f1c5fdf2527", ...

This will not mutate Objectids. It will return a new array.

It's not a answer to convert the ObjectId to string.

But if you want to find only the unique ObjectId, you can use the set object in JavaScript

According to MDN :

The Set object lets you store unique values of any type, whether primitive values or object references.

You can do:

const set1 = new Set([your array]);

本文标签: javascriptHow to convert an array of mongoose ObjectIds into an array of stringsStack Overflow