如何将JavaScript数组对象转换为数组?

时间:2018-01-05 05:57:20

标签: javascript

我需要将对象数组转换为对象中存在的键的数组。所需的输出应该是[用户名数组]。

[  
   {  
      "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"
   }
]

3 个答案:

答案 0 :(得分:1)

您可以使用map功能。

array.map(x => x.UserName)

答案 1 :(得分:1)

使用地图功能:Map reference

在这种情况下:

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;
});

答案 2 :(得分:0)

请检查此更新代码,将用户名转换为数组值



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);