gpt4 book ai didi

javascript - 返回对象数组

转载 作者:行者123 更新时间:2023-12-02 13:46:16 25 4
gpt4 key购买 nike

我有这个功能:

buildDeck = () => {
let suit = ['C', 'D', 'H', 'S'];
let rank = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
let deck = [];
for (var i=0; i < rank.length; i++){
for (var k=0; k < suit.length; k++){
deck.push(rank[i] + suit[k])
}
};
return deck;
}

这会返回一个像['2C', '4D', etc]这样的数组我希望它返回一组对象,因此每个对象内部都是:例如,花色:'C',等级:4如何将字符串数组转换为对象数组,并将等级和花色作为每个对象中的属性?

最佳答案

试试这个

buildDeck = () => {
let suit = ['C', 'D', 'H', 'S'];
let rank = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
let deck = [];
for (var i=0; i < rank.length; i++){
for (var k=0; k < suit.length; k++){
deck.push({ rank: rank[i], suit: suit[k]})
}
};
return deck;
}

关于javascript - 返回对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41382359/

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