按特定顺序对阵列进行排序



>我有一个数组,想按特定顺序对数组进行排序

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"]

您需要直接获取值并注意拼写。

排序对给定数组进行排序。

Mabe 您以 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);

试试下面的这段代码。

// 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);

最新更新