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

时间:2012-03-07 18:07:32

标签: javascript json

当我在一个名为“source”的变量上调试console时,我得到以下内容:

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

如果我在console.logging之前先将其字符串化:

JSON.stringify(source);

我明白了:

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

我有什么方法可以将源代码转换为如下所示的数组:

["Yahoo", "yahtzee", "Yakov Smirnoff", etc...]

基本上我只需要“名称”而不是“值”,我需要在一个数组中使用它们。怎么办呢?

5 个答案:

答案 0 :(得分:9)

作为其他答案的替代方案,您可以使用Array.prototype.map。但请注意,它是相当新的,在旧版浏览器中不可用 - 对于这些,我建议使用es5-shim

var names = source.map(function(item) { return item.name });

更新:使用ES6,它看起来像这样:

const names = source.map(item => item.name)

答案 1 :(得分:8)

只需使用简单的for循环:

var arr = [];
for (var i = 0; i < objects.length; i++) {
    arr.push(objects[i].name);
}

答案 2 :(得分:2)

您的外部结构是一个包含一堆对象文字的数组。通过简单的for循环将每个对象文字的name属性推送到新数组。

var outArray = [];
for (i=0; i<inArray.length; i++ {
  outArray.push(inArray[i].name);
}

console.log(outArray);

答案 3 :(得分:2)

更新2020年7月6日

在ES6中,有以下三种(3)变体将对象转换为数组:

const MyObjects = {   key1: 'value 1',   key2: 'value 2', };

// Method 1: Converts the keys to Array
// --------------------------------------

Object.keys(MyObjects);
// ['key1', 'key2']

// Method 2 Converts the Values to Array
// --------------------------------------

Object.values(MyObjects);
// ['value 1', 'value 2']

// Method 3 Converts both Values and Keys
// --------------------------------------

Object.entries(MyObjects);
// [ ['key1', 'value 1'], ['key2', 'value 2'] ]

将数组转换回对象可以完成以下操作:

const array = [  ['one', 1],   ['two', 2], ];

Object.fromEntries(array);

// { one: 1, two: 2 }

答案 4 :(得分:0)

var result = [];

for (var i in source)
  result.push(source[i].name);