问题
I am needing to pull all of the names out of the object below and store into an array. the reason I am needing this is to be able to use the JQuery tag-it plugin for auto complete. So if you know of a better way I can do this other than creating a new array I am all ears.
{
"user": [
{
"title": "boss",
"name": "scott"
},
{
"title": "janitory",
"name": "bob"
},
{
"title": "dictation",
"name": "betty"
},
{
"title": "vp",
"name": "ted"
}
]
}
Expected Output = ["scott","bob","betty","ted"]
回答1:
var arr = [{name:'Abe'},{name:'Bart'},{name:'Cletus'},{name:'Duffman'}];
var names = arr.map(function(a) { return a.name; });
names contains ["Abe", "Bart", "Cletus", "Duffman"]
.
来源:https://stackoverflow.com/questions/22718199/extract-object-values-into-an-array