我想要做的是对这些数组的元素进行排序:
name[];
standing[];
value[];
maxvalue[];
id[];
repstring[];
通过排序数组:
var SortArr = [1,2,4,6,3,5,7,8,12,11,10,9...etc]
一直到106,这是这些数组的长度。
此排序数组旨在成为以自定义方式重新排列数据的关键,而非按字母顺序排列。
有效地执行此操作的方法是什么?我已经看过尝试将上面的数组合并到一个对象中,因为我一直在阅读这可能是最好的方法(?),但我无法弄清楚如何。我对javascript和编码很新,所以任何指针都是受欢迎的。感谢
这是我的代码:
}
//...
//this is all within a function called rep()
//Declare things
var name = [ ];
var standing = [ ];
var value = [ ];
var maxvalue = [ ];
var id = [ ];
//For converting repstanding to strings
var reputation = new Array ("Hated", "Hostile", "Unfriendly", "Neutral", "Friendly", "Honored", "Revered", "Exalted");
//array holding converted strings for each faction
var repstring = [ ];
var count = toon.reputation.length; //this value is 106
//Populate arrays
for (i = 0; i < count; i++)
{
name[i] = toon.reputation[i].name; //string
standing[i] = toon.reputation[i].standing; //number from 0-7
value[i] = toon.reputation[i].value; // number from 0-21000
maxvalue[i] = toon.reputation[i].max; //number from 3000-21000
id[i] = toon.reputation[i].id //number from 1-1741
}
//Convert standing numbers to reputation string
for (i=0; i < count; i++)
{
repstring[i] = reputation[standing[i]];
}
//Create output array
var repInfo = new Array(
name, standing, repstring, value, maxvalue, id, SortArr
)
//Output array
return repInfo;
}
编辑: 这是我的函数输出的示例: (注意:这只显示每个数组的值1-5,每个数组中有106个值)
Name Standing Repstring Value MaxValue ID SortArr
The Black Prince 3 Neutral 2800 3000 1359 5
The Sons of Hodir 0 Hated 0 36000 1119 3
Booty Bay 3 Neutral 1875 3000 21 2
Zandalar Tribe 3 Neutral 0 3000 270 1
The Oracles 3 Neutral 0 3000 1105 4
好的,我想要做的是重新排列这些,以便它们由SortArr排序,所以它看起来像这样:
Name Standing Repstring Value MaxValue ID SortArr
Zandalar Tribe 3 Neutral 0 3000 270 1
Booty Bay 3 Neutral 1875 3000 21 2
The Sons of Hodir 0 Hated 0 36000 1119 3
The Oracles 3 Neutral 0 3000 1105 4
The Black Prince 3 Neutral 2800 3000 1359 5
答案 0 :(得分:0)
如果我正确理解了这个问题,您希望在比较器函数中使用SortArray
中的数字索引进行排序。
// indices: 0 1 2 3 4 5 6
// When you see a 1, treat it like 0 (its index in array) in the comparator function
// When you see a 2, treat it like 2 (its index in array)
var desiredSortOrder = [1, 3, 2, 7, 5, 4, 6];
var toSort = [1, 7, 2, 3, 4, 5, 6, 1, 2, 3, 7, 4, 5, 6];
// Map where the key is the number, and the value is the index in the desiredSortOrder array above
var mapToIndex = {};
desiredSortOrder.forEach((int, index) => mapToIndex[int] = index);
toSort.sort((a, b) => mapToIndex[a] - mapToIndex[b]);
console.log(toSort);
// [1, 1, 3, 3, 2, 2, 7, 7, 5, 5, 4, 4, 6, 6]
&#13;
答案 1 :(得分:0)
根据您上面所说的内容,我会做以下事情......
var SortArr = [1, 2, 4, 6, 3, 5, 7, 8, 12, 11, 10, 9];
var collatedArray = [];
for (var i = 0; i < SortArr.length; i++) {
index = SortArr[i];
var collated = {
name : name[index],
standing : standing[index],
value : value[index],
maxvalue : maxvalue[index],
id : id[index],
repstring : repstring[index]
}
collatedArray.push(collated);
}
// Do what you want now with this collated array,
// which is an array of the objects you require.
// You can loop through them and access the properties
// via obj.id, obj.repstanding, etc.
E.g。
function getPlayer(id) {
var player = null;
for (var i = 0; i < collatedArray.length; i++) {
if (collatedArray[i].id == id) {
player = collatedArray[i];
break;
}
}
return player;
}
此后只需访问玩家对象的属性......
player.id
player.repstanding
// etc...
如果您需要返回的对象是不可变的,那么改变此代码以从对象中删除公共变量并允许通过getter方法进行访问将非常简单。
你应该考虑你的模型并存储一组玩家(或者上面代表的任何东西),而不是为那个特定模型的每个属性存储数组。
答案 2 :(得分:0)
您可以使用Array#forEach()
并使用新索引构建新数组。
var array = ['The Black Prince', 'The Sons of Hodir', 'Booty Bay', 'Zandalar Tribe', 'The Oracles'],
sortOrder = [5, 3, 2, 1, 4];
function sort(array, sortOrder) {
var result = [];
sortOrder.forEach(function (a, i) {
result[a - 1] = array[i];
});
return result;
}
document.write('<pre>' + JSON.stringify(sort(array, sortOrder), 0, 4) + '</pre>');
只需提示一下,您可以按行组织数据,其中一个项目的所有数据都收集在对象中,如下所示:
[
{
"name": "The Black Prince",
"standing": 3,
"repstring": "Neutral",
"value": 2800,
"maxValue": 3000,
"id": 1359,
"sortOrder": 5
},
{
"name": "The Sons of Hodir",
"standing": 0,
"repstring": "Hated",
"value": 0,
"maxValue": 36000,
"id": 1119,
"sortOrder": 3
},
// ...
]