gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:45:18 25 4
gpt4 key购买 nike

当我在名为“source”的变量上进行 console.log 时,我得到以下信息:

[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...]

基本上我只需要“名称”而不是“值”,并且我需要将它们全部放在一个数组中。如何做到这一点?

最佳答案

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

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

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

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

关于javascript - 如何在 JavaScript 中将对象转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com