gpt4 book ai didi

javascript - 如何将数组中的对象作为带逗号的字符串返回?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:31 24 4
gpt4 key购买 nike

变量列表 = [ { 编号:1234, 商店:“商店1” }, { 编号:4312, 商店:'shop2' } ];

我只希望对象数组中的 id 属性返回为 "1234,4312"。我能怎么做?

最佳答案

更简单:

var list = [ { id : 1234, shop : 'shop1' }, { id : 4312, shop : 'shop2' } ];
ids = list.map(function(obj){
return obj.id
})

如果您特别需要一个字符串,请将 .toString() 添加到 map 调用的末尾:

ids = list.map(function(obj){
return obj.id
}).toString()

关于javascript - 如何将数组中的对象作为带逗号的字符串返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24070317/

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