How do I convert an object to an array in JavaScript?
When I console.log on a variable called "source" I get this:
[Object {
name = "Yahoo", value = "yahoo"
},
Object {
name = "yahtzee", value = "yahtzee"
},
Object {
name = "Yakov Smirnoff", value = "yakov-smirnoff"
},
Object {
name = "Yarbrough", value = "yarbrough"
},
Object {
name = "yard sales", value = "yard-sales"
},
Object {
name = "yarmulke", value = "yarmulke"
},
Object {
name = "yawning", value = "yawning"
},
Object {
name = "Yeah Yeah Yeahs", value = "yeah-yeah-yeahs"
},
Object {
name = "Yeardly Smith", value = "yeardly-smith"
},
Object {
name = "YearOne", value = "yearone"
},
Object {
name = "Yeasayer", value = "yeasayer"
},
Object {
name = "yelle", value = "yelle"
},
Object {
name = "yelling", value = "yelling"
},
Object {
name = "yellowpages", value = "yellowpages"
},
Object {
name = "yellowstone", value = "yellowstone"
},
Object {
name = "yemen", value = "yemen"
},
Object {
name = "Yeoman", value = "yeoman"
},
Object {
name = "Yes Dear", value = "yes-dear"
},
Object {
name = "Yes Men", value = "yes-men"
},
Object {
name = "yeti", value = "yeti"
},
Object {
name = "yiddish", value = "yiddish"
},
Object {
name = "ymca", value = "ymca"
},
Object {
name = "yodeling", value = "yodeling"
},
Object {
name = "yoga", value = "yoga"
},
Object {
name = "yogurt", value = "yogurt"
},
Object {
name = "Yoko Ono", value = "yoko-ono"
},
Object {
name = "Yo-Landi Vi$$er", value = "yo-landi-vier"
},
Object {
name = "yo momma", value = "yo-momma"
},
Object {
name = "YONKERS", value = "yonkers"
},
Object {
name = "yosemite", value = "yosemite"
},
Object {
name = "yoshimoto", value = "yoshimoto"
},
Object {
name = "Yoshio Yoda", value = "yoshio-yoda"
},
Object {
name = "you got served", value = "you-got-served"
},
Object {
name = "YourDailyLaughz", value = "yourdailylaughz"
},
Object {
name = "yourfavorite", value = "yourfavorite"
},
Object {
name = "Yoursie Thomas", value = "yoursie-thomas"
},
Object {
name = "You Suck at Photoshop", value = "you-suck-at-photoshop"
},
Object {
name = "Youth in Revolt", value = "youth-in-revolt"
},
Object {
name = "Youtube Next Lab", value = "youtube-next-lab"
},
Object {
name = "YSAP", value = "ysap"
},
Object {
name = "yt3d:aspect=16:9", value = "yt3daspect169"
},
Object {
name = "yt3d:enable=true", value = "yt3denabletrue"
},
Object {
name = "yt3d:metadata=user", value = "yt3dmetadatauser"
},
Object {
name = "yt3d:swap=true", value = "yt3dswaptrue"
},
Object {
name = "y tu mama tambien", value = "y-tu-mama-tambien"
},
Object {
name = "YuGiOh", value = "yugioh"
},
Object {
name = "Yugo Koral", value = "yugo-koral"
},
Object {
name = "Yuri Baranovsky", value = "yuri-baranovsky"
},
Object {
name = "Yvan Attal", value = "yvan-attal"
},
Object {
name = "Yvette Nicole Brown", value = "yvette-nicole-brown"
},
Object {
name = "yvonne de carlo", value = "yvonne-de-carlo"
}]
If I build it first before console.logging:
JSON.stringify(source);
I get:
[{
"name": "Yahoo",
"value": "yahoo"
}, {
"name": "yahtzee",
"value": "yahtzee"
}, {
"name": "Yakov Smirnoff",
"value": "yakov-smirnoff"
}, {
"name": "Yarbrough",
"value": "yarbrough"
}, {
"name": "yard sales",
"value": "yard-sales"
}, {
"name": "yarmulke",
"value": "yarmulke"
}, {
"name": "yawning",
"value": "yawning"
}, {
"name": "Yeah Yeah Yeahs",
"value": "yeah-yeah-yeahs"
}, {
"name": "Yeardly Smith",
"value": "yeardly-smith"
}, {
"name": "YearOne",
"value": "yearone"
}, {
"name": "Yeasayer",
"value": "yeasayer"
}, {
"name": "yelle",
"value": "yelle"
}, {
"name": "yelling",
"value": "yelling"
}, {
"name": "yellowpages",
"value": "yellowpages"
}, {
"name": "yellowstone",
"value": "yellowstone"
}, {
"name": "yemen",
"value": "yemen"
}, {
"name": "Yeoman",
"value": "yeoman"
}, {
"name": "Yes Dear",
"value": "yes-dear"
}, {
"name": "Yes Men",
"value": "yes-men"
}, {
"name": "yeti",
"value": "yeti"
}, {
"name": "yiddish",
"value": "yiddish"
}, {
"name": "ymca",
"value": "ymca"
}, {
"name": "yodeling",
"value": "yodeling"
}, {
"name": "yoga",
"value": "yoga"
}, {
"name": "yogurt",
"value": "yogurt"
}, {
"name": "Yoko Ono",
"value": "yoko-ono"
}, {
"name": "Yo-Landi Vi$$er",
"value": "yo-landi-vier"
}, {
"name": "yo momma",
"value": "yo-momma"
}, {
"name": "YONKERS",
"value": "yonkers"
}, {
"name": "yosemite",
"value": "yosemite"
}, {
"name": "yoshimoto",
"value": "yoshimoto"
}, {
"name": "Yoshio Yoda",
"value": "yoshio-yoda"
}, {
"name": "you got served",
"value": "you-got-served"
}, {
"name": "YourDailyLaughz",
"value": "yourdailylaughz"
}, {
"name": "yourfavorite",
"value": "yourfavorite"
}, {
"name": "Yoursie Thomas",
"value": "yoursie-thomas"
}, {
"name": "You Suck at Photoshop",
"value": "you-suck-at-photoshop"
}, {
"name": "Youth in Revolt",
"value": "youth-in-revolt"
}, {
"name": "Youtube Next Lab",
"value": "youtube-next-lab"
}, {
"name": "YSAP",
"value": "ysap"
}, {
"name": "yt3d:aspect=16:9",
"value": "yt3daspect169"
}, {
"name": "yt3d:enable=true",
"value": "yt3denabletrue"
}, {
"name": "yt3d:metadata=user",
"value": "yt3dmetadatauser"
}, {
"name": "yt3d:swap=true",
"value": "yt3dswaptrue"
}, {
"name": "y tu mama tambien",
"value": "y-tu-mama-tambien"
}, {
"name": "YuGiOh",
"value": "yugioh"
}, {
"name": "Yugo Koral",
"value": "yugo-koral"
}, {
"name": "Yuri Baranovsky",
"value": "yuri-baranovsky"
}, {
"name": "Yvan Attal",
"value": "yvan-attal"
}, {
"name": "Yvette Nicole Brown",
"value": "yvette-nicole-brown"
}, {
"name": "yvonne de carlo",
"value": "yvonne-de-carlo"
}]
Is there a way to convert the source to an array that looks like this:
["Yahoo", "yahtzee", "Yakov Smirnoff", etc...]
Basically I just want the "name", not the "value" and I want them all in one array. How can I do that?
+3
source to share
4 answers
As an alternative to the other answers, you can use Array.map . Note, however, that it is fairly new and not available in older browsers - I recommend es5-shim for that .
var names = source.map(function(item) { return item.name });
Update: As of ES6 it will look something like this:
const names = source.map(item => item.name)
+8
source to share