配列があり、特定の順序で配列を並べ替えたい
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"]
値を直接取得し、スペルに注意を払う必要があります。
並べ替えは、指定された配列を並べ替えます。
注文オブジェクトの値をゼロではなく1から開始し、デフォルト値を使用して不明な値を目的の位置に並べ替えます。
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);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加