Javascript 对对象数组重新排序,使数组的每个位置都有一个特定的键/值对
我有一个对象数组,看起来像这样:Javascript 对对象数组重新排序,使数组的每个位置都有一个特定的键/值对,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有一个对象数组,看起来像这样: var players = [ { "imagePos1": '', "imagePos2": 'test', "imagePos3": '', "imagePos4": '' }, { "imagePos1": '', "imagePos2": 'test', "imagePos3": 'test', "imagePos4": '' }, { "imagePos1":
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": ''
}
];
我需要重新组织players数组,以便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 noinfinitelopspLz=0;
函数findDuds(){
如果(noinfinitelopsplz>256){
console.log('不会发生')
返回false;
}否则{
//添加一个以确保递归函数不会永远运行
Noinfinitelopsplz++
对于(变量i=0;i
根据您的输入数据,我们可以简单地执行以下操作。假设空值表示为空字符串;如果需要,您可以修改检查
var玩家=[{
“imagePos1”:”,
“imagePos2”:“测试”,
“imagePos3”:“”,
“imagePos4”:”
}, {
“imagePos1”:”,
“imagePos2”:“测试”,
“imagePos3”:“测试”,
“imagePos4”:”
}, {
“imagePos1”:”,
“imagePos2”:“测试”,
“imagePos3”:“”,
“imagePos4”:“测试”
}, {
“imagePos1”:“测试”,
“imagePos2”:“”,
“imagePos3”:“测试”,
“imagePos4”:”
}];
//选择二进制,存储为整数,创建可能的组合部件
var-bin=[];
var com=[];
for(玩家中的var i){
bin[i]=0;
com[i]=[];
对于(玩家中的道具[i]){
bin[i]>=1){
如果(n&1){
com[i].push(1您希望找到满足一组约束的玩家的排列
如果从正确答案和池中移除第一个玩家,则正确答案的其余部分是满足类似约束集的池的排列。序列长度也很小。因此,我们可以使用递归搜索
对于位置1以外的每个玩家,我们查看是否有剩余玩家从2到4的排列。如果有,我们返回候选玩家加上该排列。如果没有,将抛出异常,然后我们转到下一个候选玩家。如果没有候选玩家,或者我们尝试了所有候选玩家,则问题无法解决。测试对于位置2,ing是类似的,因此代码是相同的
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": ''
}
];
/*
* Returns a function which tests whether the player passed to it has
* an image in the given position.
*/
function has(index) {
return function (player) {
return player["imagePos"+index];
}
}
/*
* Orders elements in `rest` numbered from from to `to`.
* `start` is prepended.
*/
function order(from, to, start, rest) {
/* check if we are done */
if (from > to) {
return start;
}
/* find elements that might come next */
var candidates = rest.filter(has(from));
var len = candidates.length;
/* Try each candidate. */
for (var i = 0; i < len; i++) {
var candidate = candidates[i],
/* elements that may come later assuming this candidate comes next */
remainder = rest.filter(function (el) { return el !== candidate; }),
/* new prefix including this candidate */
newStart = start.concat(candidate);
try {
/* order the remaining candidates by the rest of the numbers */
return order(1+from, to, newStart, remainder);
} catch (e) {
/* on failure we try the next candidate */
}
}
/* If we get here we tried all the candidates (possibly 0) and none of them worked. */
throw "unsatisfiable";
}
order(1, 4, [], players);
var玩家=[
{
“imagePos1”:”,
“imagePos2”:“测试”,
“imagePos3”:“”,
“imagePos4”:”
},
{
“imagePos1”:”,
“imagePos2”:“测试”,
“imagePos3”:“测试”,
“imagePos4”:”
},
{
“imagePos1”:”,
“imagePos2”:“测试”,
“imagePos3”:“”,
“imagePos4”:“测试”
},
{
“imagePos1”:“测试”,
“imagePos2”:“”,
“imagePos3”:“测试”,
“imagePos4”:”
}
];
/*
*返回一个函数,该函数测试传递给它的播放机是否具有
*处于给定位置的图像。
*/
函数具有(索引){
返回功能(播放器){
返回播放器[“imagePos”+索引];
}
}
/*
*对“rest”中的元素进行排序,编号从到。
*“开始”是有前缀的。
*/
功能顺序(从、到、开始、休息){
/*检查我们是否完成了*/
如果(从>到){
返回启动;
}
/*查找下一个可能出现的元素*/
var候选者=rest.filter(has(from));
var len=候选项长度;
/*试试每个候选人*/
对于(变量i=0;i
您能否更具体地定义确定一个对象如何优先于另一个对象的规则?例如,在第一个和最后一个属性中具有值的对象在排序输出中的位置如何?重要的是数组中的第一个项对于imagePos1键具有一些值,而数组中的第二个项具有一些值imagePos2键的一些值等等。但是如果两个对象在同一个属性中有一个值,那么哪个值优先?不要紧-两者都可以放在那个位置。顺序只在每个对象都有一个与其索引相关的值时才重要。
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": ''
}
];
/*
* Returns a function which tests whether the player passed to it has
* an image in the given position.
*/
function has(index) {
return function (player) {
return player["imagePos"+index];
}
}
/*
* Orders elements in `rest` numbered from from to `to`.
* `start` is prepended.
*/
function order(from, to, start, rest) {
/* check if we are done */
if (from > to) {
return start;
}
/* find elements that might come next */
var candidates = rest.filter(has(from));
var len = candidates.length;
/* Try each candidate. */
for (var i = 0; i < len; i++) {
var candidate = candidates[i],
/* elements that may come later assuming this candidate comes next */
remainder = rest.filter(function (el) { return el !== candidate; }),
/* new prefix including this candidate */
newStart = start.concat(candidate);
try {
/* order the remaining candidates by the rest of the numbers */
return order(1+from, to, newStart, remainder);
} catch (e) {
/* on failure we try the next candidate */
}
}
/* If we get here we tried all the candidates (possibly 0) and none of them worked. */
throw "unsatisfiable";
}
order(1, 4, [], players);