-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"]

Mohammed Gadiwala
  • 1,923
  • 2
  • 18
  • 26
  • if you are a programmar then try to google it first... – Banik Feb 16 '16 at 07:14
  • Possible duplicate of [Iterate through associative array using sequential for loop](http://stackoverflow.com/questions/15809366/iterate-through-associative-array-using-sequential-for-loop) – Desert Rose Feb 16 '16 at 07:40

3 Answers3

1

you can use the map function

result = myArray.map(function(item){ return item.userid; })
eltonkamami
  • 5,134
  • 1
  • 22
  • 30
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);
Altenrion
  • 764
  • 3
  • 14
  • 35
Divyesh Savaliya
  • 2,692
  • 2
  • 18
  • 37
0

A simpler way would be

const results = myArray.map(item => item.userId);
Nishant Jain
  • 197
  • 1
  • 9