我有一个数组,想按特定顺序对数组进行排序
var orderedObj = {
"1st Presentation / Meeting": 0,
"Follow-On Meetings": 1,
"Hold\/Uncategorized": 2,
"MGL": 3,
"PGL": 4,
"BGL": 5,
"RGL": 6,
"SGL": 7,
"Uncategorized Leads": 8,
"Identified Opportunities": 9,
"QO under evaluation": 10
};
const typobj = ["Uncategorized lead", "Hold/Uncategorized", "RGL", "PGL", "MGL", "QO under evaluation", "Reaches", "Identified Opportunities", "BGL", "Back to marketing", "SGL", "Follow-On Meetings", "1st Presentation / Meeting"];
var typesOrd = typobj.sort((a, b) => orderedObj[a.label] - orderedObj[b.label]);
console.log(typesOrd);
无法排序以将输出作为["Uncategorized lead", "Hold/Uncategorized", "RGL", "PGL", "MGL", "QO under evaluation", "Reaches", "Identified Opportunities", "BGL", "Back to marketing", "SGL", "Follow-On Meetings", "1st Presentation / Meeting"]
答案 0 :(得分:1)
您需要直接获取值并注意拼写。
排序对给定的数组进行排序。
也许您以1(而不是0)开始订购对象的值,并使用默认值将未知值排序到所需位置。
var orderedObj = {
"1st Presentation / Meeting": 0,
"Follow-On Meetings": 1,
"Hold\/Uncategorized": 2,
"MGL": 3,
"PGL": 4,
"BGL": 5,
"RGL": 6,
"SGL": 7,
"Uncategorized Leads": 8,
"Identified Opportunities": 9,
"QO under evaluation": 10
};
const typobj = ["Uncategorized Leads", "Hold/Uncategorized", "RGL", "PGL", "MGL", "QO under evaluation", "Reaches", "Identified Opportunities", "BGL", "Back to marketing", "SGL", "Follow-On Meetings", "1st Presentation / Meeting"];
typobj.sort((a, b) => orderedObj[a] - orderedObj[b]);
console.log(typobj);
答案 1 :(得分:0)
请尝试以下代码。
// RAW
var orderedObj = {
"1st Presentation / Meeting": 0,
"Follow-On Meetings": 1,
"Hold\/Uncategorized": 2,
"MGL": 3,
"PGL": 4,
"BGL": 5,
"RGL": 6,
"SGL": 7,
"Uncategorized Leads": 8,
"Identified Opportunities": 9,
"QO under evaluation": 10
};
//Target order
const typobj = ["Uncategorized Leads", "Hold/Uncategorized", "RGL", "PGL", "MGL", "QO under evaluation", "Reaches", "Identified Opportunities", "BGL", "Back to marketing", "SGL", "Follow-On Meetings", "1st Presentation / Meeting"];
var new_obb={};
typobj.forEach(function(item){
new_obb[item]=orderedObj[item];
});
// Final Output
console.log(new_obb);