gpt4 book ai didi

javascript - 随机排列数组中的键

转载 作者:行者123 更新时间:2023-12-01 01:19:24 26 4
gpt4 key购买 nike

我有一个数组

var array = [{
"Abigail": ["I feel that anyone breaking the law deserves what they get", "1"],
"Alexandra": ["There comes a time that you can just", "5"],
"Alexis": ["She looks mean anyways.", "2"]
}, {
"Abigail": ["Bet she wishes she hadn't broken the law", "1"],
"Alexandra": ["Bad girls don't wear scarfs.", "5"],
"Alexis": ["That's the look of someone who has lost hope in humanity.", "5"]
}, {
"Abigail": ["She probably wanted this to happen.", "1"],
"Alexandra": ["What did she do to warrent all of this attention?", "5"],
"Alexis": ["I think people just going about it all wrong.", "5"]
}]

我只想打乱数组中的名称,但希望保持名称的顺序相同。

我尝试了很多东西,包括 Fisher-Yates Shuffle 的这段代码 https://bost.ocks.org/mike/shuffle/ 。我只能在较低级别的列表中洗牌,而不能在名称中洗牌。

期望结果的示例:

var arrayShuffled = [{
"Abigail": ["I feel that anyone breaking the law deserves what they get", "1"],
"Alexis": ["She looks mean anyways.", "2"],
"Alexandra": ["There comes a time that you can just stop screaming in peoples ears.", "5"]
}, {
"Abigail": ["Bet she wishes she hadn't broken the law", "1"],
"Alexis": ["That's the look of someone who has lost hope in humanity.", "5"],
"Alexandra": ["Bad girls don't wear scarfs.", "5"]
}, {
"Abigail": ["She probably wanted this to happen.", "1"],
"Alexis": ["I think people just going about it all wrong.", "5"],
"Alexandra": ["What did she do to warrent all of this attention?", "5"]
}]

最佳答案

与 Laurens 的差别不大,但它在按键上实现了 Knuth(或 Fisher-Yates)洗牌。我还使用了 ES6 语法。

var objList = [{
"Abigail": ["I feel that anyone breaking the law deserves what they get", "1"],
"Alexandra": ["There comes a time that you can just", "5"],
"Alexis": ["She looks mean anyways.", "2"]
}, {
"Abigail": ["Bet she wishes she hadn't broken the law", "1"],
"Alexandra": ["Bad girls don't wear scarfs.", "5"],
"Alexis": ["That's the look of someone who has lost hope in humanity.", "5"]
}, {
"Abigail": ["She probably wanted this to happen.", "1"],
"Alexandra": ["What did she do to warrent all of this attention?", "5"],
"Alexis": ["I think people just going about it all wrong.", "5"]
}]

console.log(shuffleArrayItemKeys(objList)) // Print the shuffled list.

function shuffleArrayItemKeys(arr) {
var keys = knuthShuffle(Object.keys(arr[0]))
return arr.map((item) => {
return keys.reduce((result, key, index) => {
return { ... result, ... { [key] : item[key] } }
}, {})
})
}

function knuthShuffle(arr) {
var currIndex = arr.length, randIndex
while (currIndex !== 0) {
randIndex = Math.floor(Math.random() * currIndex)
currIndex--
__swap__(arr, currIndex, randIndex)
}
return arr
}

/**
* @private
*/
function __swap__(arr, index1, index2) {
let tmp = arr[index1]
arr[index1] = arr[index2]
arr[index2] = tmp
}
.as-console-wrapper {
top: 0;
max-height: 100% !important;
}

注意:使用 Object.assign(result, { [key] : item[key] }) 可以代替展开运算符,例如{ ... result, ... { [key] : item[key] } } 作为对浏览器更友好的替代方案。

关于javascript - 随机排列数组中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54374592/

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