-4

I need to convert the array of objects into an array of a key present in the object. The desired output should be an [array of username].

[  
   {  
      "UserID":"66e7ba60-9ad2-41a1-83f2-f8cf97afb97d",
      "UserName":"Karthik Kumar",
      "UserEmail":"karthikbuyer@365media.in",
      "UserType":"2"
   },
   {  
      "UserID":"292716bd-5386-4371-8ef9-e8ebbebb432f",
      "UserName":"karthik kumar",
      "UserEmail":"karthikvendor@365media.in",
      "UserType":"3"
   },
   {  
      "UserID":"d23eb3e0-1bcd-429f-9268-6cdd95772b08",
      "UserName":"karthikeyan rajendran",
      "UserEmail":"kartik1235@gmail.com",
      "UserType":"3"
   }
]
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
karty
  • 187
  • 1
  • 1
  • 10

3 Answers3

1

You can use the map function.

array.map(x => x.UserName)
Kraylog
  • 7,383
  • 1
  • 24
  • 35
1

Use map function: Map reference

In this case:

var arr = [  
   {  
      "UserID":"66e7ba60-9ad2-41a1-83f2-f8cf97afb97d",
      "UserName":"Karthik Kumar",
      "UserEmail":"karthikbuyer@365media.in",
      "UserType":"2"
   },
   {  
      "UserID":"292716bd-5386-4371-8ef9-e8ebbebb432f",
      "UserName":"karthik kumar",
      "UserEmail":"karthikvendor@365media.in",
      "UserType":"3"
   },
   {  
      "UserID":"d23eb3e0-1bcd-429f-9268-6cdd95772b08",
      "UserName":"karthikeyan rajendran",
      "UserEmail":"kartik1235@gmail.com",
      "UserType":"3"
   }
]

var value = arr.map(function (element) {
    return element.UserName;
});
Vishant dhandha
  • 495
  • 7
  • 19
Javasamurai
  • 666
  • 7
  • 21
0

Please check this updated code where it converts usernames in to array values

var myarray = [  
   {  
      "UserID":"66e7ba60-9ad2-41a1-83f2-f8cf97afb97d",
      "UserName":"Karthik Kumar",
      "UserEmail":"karthikbuyer@365media.in",
      "UserType":"2"
   },
   {  
      "UserID":"292716bd-5386-4371-8ef9-e8ebbebb432f",
      "UserName":"karthik kumar",
      "UserEmail":"karthikvendor@365media.in",
      "UserType":"3"
   },
   {  
      "UserID":"d23eb3e0-1bcd-429f-9268-6cdd95772b08",
      "UserName":"karthikeyan rajendran",
      "UserEmail":"kartik1235@gmail.com",
      "UserType":"3"
   }
]

var value = myarray.map(x => x.UserName)

console.log(value);