-1

So I want to convert this array retrieved from a database to just an array of string values:

Array:[{"userid":"c"},{"userid":"d"}]

Expected results:["c","d"]

2

3 Answers 3

1

you can use the map function

result = myArray.map(function(item){ return item.userid; })
Sign up to request clarification or add additional context in comments.

Comments

0

If you need this conversions on javascript side, you should keep in mind, that if there would be a lot of items, the performance might be rather bad. May be it would be better to change logick of gathering the first array.

For conversion you could try this:

Data = [{"userid":"c"},{"userid":"d"}];
arr = [];
for(i in Data){
   arr.push(Data[i].userid);
}
console.log(arr);

Comments

0

A simpler way would be

const results = myArray.map(item => item.userId);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.