按特定顺序对数组进行排序

用户名

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

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

妮娜·斯科茨(Nina Scholz)

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章