gpt4 book ai didi

令人头疼的 Javascript 数组排序

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

我有一个看起来类似于此的对象数组:

[
{
id : 1,
position: false
},
{
id: 2,
position: false
},
{
id: 3,
position: 4
},
{
id: 4,
position: false
},
{
id: 5,
position: 2
},
{
id: 6,
position: 5
},
{
id: 7,
position: 3
},
{
id: 8,
position: 1
},
]

我想使用每个对象的位置属性(升序)对这个数组进行排序,这样数组看起来像这样:

[
{
id: 8,
position: 1
},
{
id: 5,
position: 2
},
{
id: 7,
position: 3
},
{
id: 3,
position: 4
},
{
id: 6,
position: 5
},
{
id: 1,
position: false
},
{
id: 2,
position: false
},
{
id: 4,
position: false
}
]

想法只是将位置不为 false 的对象移动到顶部,而不影响位置设置为 false 的其余对象的顺序。

看起来很简单,对吧?我已尝试通过多种方式解决此问题,但似乎没有任何可靠的方法。希望有人可以帮助我了解如何正确处理这个问题。

帮助我 Stack Overflow,你是我唯一的希望。

最佳答案

我试图添加一堆评论来帮助您遵循我的想法。这是以命令式风格实现的,并且(我希望)可以帮助说明一些关于如何处理类似问题的想法。

我使用了您的示例数据集,并重现了您正在寻找的结果。我希望这对您有所帮助,并且可以对其进行修改以适合您需要的任何数据。

I also pasted the text here .

var deepSort = function(arr){
var i;
var hasPositionKey = [];
var noPositionKey = [];
var nestedObj;
var positions = [];
var loggingPositions = {};
var positionSortedObjects = [];

// iterate through the array of objects
for(i = 0; i < arr.length; i++){
// set a variable for the individual object being considered
nestedObj = arr[i];
// if that object has a position of false
if(nestedObj.position === false){
// push false object into the array of false position objects
noPositionKey.push(nestedObj);
}
// if the object has a numbered position
if(typeof nestedObj.position === 'number'){
// push numbered position to the array of numbered position objects
hasPositionKey.push(nestedObj);
}
}

// iterate through the array of objects with positions
for(i = 0; i < hasPositionKey.length; i++){
// set a variable for the individual object being considered
nestedObj = hasPositionKey[i];
// push only the position number into an array to track positions
positions.push(nestedObj.position);
// add a key value pair of position:id to an object
loggingPositions[nestedObj.position] = nestedObj.id;
}

// sort the array that stores the positions of the non-false objects
positions.sort();
// iterate through that array and construct a final array of objects by sorted position
for(i = 0; i < positions.length; i++){
// set a variable for the positions, as encountered in order:
var num = positions[i];
// set a key for the object to be constructed
var key = loggingPositions[num];
// set a value for the object to be constructed
var value = num;
// construct a result object with object literal notation:
var result = {
id: key,
position: value
};
// push the constructed objects into the positionSortedObjects array
positionSortedObjects.push( result );
}

// return a concatenated array of the positionSortedObjects + the noPositionKey objects
return (positionSortedObjects.concat(noPositionKey));
};

var sample = [
{
id : 1,
position: false
},
{
id: 2,
position: false
},
{
id: 3,
position: 4
},
{
id: 4,
position: false
},
{
id: 5,
position: 2
},
{
id: 6,
position: 5
},
{
id: 7,
position: 3
},
{
id: 8,
position: 1
}
];

deepSort(sample);

关于令人头疼的 Javascript 数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23256951/

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