gpt4 book ai didi

javascript - 重新排序对象数组,使数组的每个位置都有一个特定的键/值对

转载 作者:行者123 更新时间:2023-11-30 00:33:53 26 4
gpt4 key购买 nike

我有一个看起来像这样的对象数组:

var players = [
{
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": '',
"imagePos4": ''
},
{
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": 'test',
"imagePos4": ''
},
{
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": '',
"imagePos4": 'test'
},
{
"imagePos1": 'test',
"imagePos2": '',
"imagePos3": 'test',
"imagePos4": ''
}
];

我需要重新组织播放器数组,以便索引 0 处的项目具有“imagePos1”键的值,索引 1 处的项目具有“imagePos2”键的值,等等第四项。所以对于上面的数组,正确的输出是:

var players = [
{
"imagePos1": 'test', // index 0
"imagePos2": '',
"imagePos3": 'test',
"imagePos4": ''
},
{
"imagePos1": '',
"imagePos2": 'test', // index 1
"imagePos3": '',
"imagePos4": ''
},
{
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": 'test', // index 2
"imagePos4": ''
},
{
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": '',
"imagePos4": 'test' // index 3
}
];

我不知道数组中的对象会是什么样子,所以我还需要考虑到对象不能以这种方式排序的可能性并输出一些消息。

这是我目前所拥有的(这很糟糕,我知道):

var objCache = {};
var noInfiniteLoopsPlz = 0;

function findDuds() {
if (noInfiniteLoopsPlz > 256) {
console.log('aint gonna happen')
return false;
} else {
// add one to this to make sure the recursive func doesn't go forever
noInfiniteLoopsPlz++
for (var i = 0; i < players.length; i++) {
// find what images don't have the image needed
if (players[i]['imagePos' + (i + 1)].length === 0) {
// find what ones do
for (var j = 0; j < players.length; j++) {
// when you find an image...
if (players[i]['imagePos' + (j + 1)].length) {
// save the object that's currently in that spot for now
objCache = players[j];
// then put the object that you're moving in its place
players[j] = players[i];

// place the saved object where the old one was
players[i] = objCache;
// see if the saved object has an image for the place that has opened up. If it hasn't, start this all over again
if (objCache['imagePos' + (i + 1)].length) {
findDuds();
}
}
}
}
}
}
}

findDuds();


console.log(players);

最佳答案

根据您输入的数据,我们可以简单地执行以下操作。假设空值表示为空字符串;如果需要,您可以修改支票。

var players = [{
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": '',
"imagePos4": ''
}, {
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": 'test',
"imagePos4": ''
}, {
"imagePos1": '',
"imagePos2": 'test',
"imagePos3": '',
"imagePos4": 'test'
}, {
"imagePos1": 'test',
"imagePos2": '',
"imagePos3": 'test',
"imagePos4": ''
}];

//Make choices binary, store as ints, create possible combination parts
var bin = [];
var com = [];
for (var i in players) {
bin[i] = 0;
com[i] = [];
for (prop in players[i]) {
bin[i] <<= 1;
bin[i] += ~~(players[i][prop].length > 0);
}
//Too lazy to write reasonably readable loops
for (var j = 0, n = bin[i]; n; j++, n >>= 1) {
if (n & 1) {
com[i].push(1 << j);
}
}
}

//The object keys part is essentially just telling us how many properties there are, we could hardcode this or whatever
var v = (1 << Object.keys(players[0]).length) - 1;
var per = [];
var match;
var max = com.length - 1;
//Create all combinations of the above
var rec = function(a, i) {
for (var j in com[i]) {
var cpy = a.slice();
cpy.push(com[i][j]);
if (i < max) {
rec(cpy, i + 1);
} else {
//We have a full combination, check if it fits
var n = cpy[0];
for (var j = 1; j < cpy.length; j++) {
n |= cpy[j];
}
//Stop at first match, you can change this if you want all matches
if (n === v) {
match = cpy;
return;
}
}
}
}
rec([], 0);

//This is just for pretty-printing
if (match === void 0) {
alert('there is no matching combination');
} else {
var out = '';
for (var i = 0; i < match.length; i++) {
for (var j = 0; match[i] < v; match[i] <<= 1, j++) {
//Empty loop body because snafucate
}
//Make both indexes 1-based for clarity
out += 'element ' + (i + 1) + ' goes to slot ' + j + '; ';
}
alert(out);
}

关于javascript - 重新排序对象数组,使数组的每个位置都有一个特定的键/值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28094887/

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