为了账号安全,请及时绑定邮箱和手机立即绑定

如何从数组中选择最高的 3 个字符串值

如何从数组中选择最高的 3 个字符串值

翻翻过去那场雪 2023-05-11 16:26:20
我有数组,其中有sequence number. 我只想从基于数组的数组中选择前 3 个,sequence numbers而不是像 "sequesnce": "9.80.709.5This is my array这样的序列号array1=[{    "orgId": 101,    "dId": 494,    "name": "Test1",    "sequesnce": "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR"},{    "orgId": 102,    "dId": 442,    "name": "Test2",    "sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",},{    "orgId": 103,    "sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",    "dId": 494,    "name": "Test3",},{    "orgId": 103,    "sequesnce": "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR",    "dId": 494,    "name": "Test3",},{    "orgId": 103,    "sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",    "dId": 494,    "name": "Test3",},我正在寻找输出topThree={"sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",},{"sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",},{"sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",},我试过这个function arrayMax(arr) {return arr.reduce(function (p, v) {  return ( p > v ? p : v );});}]var maximo = arrayMax(obj); //return the high这让我回来{    "orgId": 102,    "dId": 442,    "name": "Test2",    "sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",},此代码返回单个数组而不是三个,并且不确定在所有情况下此逻辑是否有效。
查看完整描述

3 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

您可以排序String#localeCompare并选择按点值列排序。

最后取前三项Array#slice

const

    array = [{ orgId: 101, dId: 494, name: "Test1", sequesnce: "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR" }, { orgId: 102, dId: 442, name: "Test2", sequesnce: "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR" }, { orgId: 103, sequesnce: "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR", dId: 494, name: "Test3" }, { orgId: 103, sequesnce: "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR", dId: 494, name: "Test3" }, { orgId: 103, sequesnce: "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR", dId: 494, name: "Test3" }],

    top3 = array

        .sort((a, b) => b.sequesnce.localeCompare(a.sequesnce, undefined, { numeric: true, sensitivity: 'base' }))

        .slice(0, 3)


console.log(top3);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2023-05-11
?
慕慕森

TA贡献1856条经验 获得超17个赞

如果序列较短且前导值较高,则需要在排序前扩展元组 - 这不适用于 localeCompare


const maxLen = 4;

const expandTuple = tpl => {

  tpl = tpl.split('.');

  while (tpl.length < maxLen) tpl.unshift("000");

  return tpl.map(x => x.padStart(3, "0")).join('.');

};

const array1 = [{ "orgId": 101, "dId": 494, "name": "Test1",

    "sequence": "6.408.06.0 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR" },{"orgId": 102,"dId": 442,"name": "Test2",

    "sequence": "9.80.709.5 Sequence Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",},{"orgId": 103,

    "sequence": "99.1.1 NOTE THIS IS SHORTER Sequence Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR","dId": 494,"name": "Test3",},{"orgId": 103,

    "sequence": "8.208.409.0 Sequence Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR","dId": 494,"name": "Test3",},{"orgId": 103,

    "sequence": "9.408.90.3 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR","dId": 494,   "name": "Test3",  }

]



const three = array1.sort((a,b) => {

  const aTub = a.sequence.split(" ")[0];

  const bTub = b.sequence.split(" ")[0];

  if (expandTuple(aTub)<expandTuple(bTub)) return 1

  if (expandTuple(aTub)>expandTuple(bTub)) return -1

  return 0;

}).slice(0,3)

console.log(three)


// ---- compare other solution which fails on the shorter sequence


console.log(array1

  .sort((a, b) => b.sequence.localeCompare(a.sequence, undefined, { numeric: true, sensitivity: 'base' }))

  .slice(0, 3)

)


查看完整回答
反对 回复 2023-05-11
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

首先,使用Array.map,您可以仅生成项目对象数组sequesne。

并使用Array.sort,您可以按降序对数组进行排序。

并使用Array.splice,您可以从排序数组中提取前 3 项。

const array1 = [{

    "orgId": 101,

    "dId": 494,

    "name": "Test1",

    "sequesnce": "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR"

  },

  {

    "orgId": 102,

    "dId": 442,

    "name": "Test2",

    "sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",

  },

  {

    "orgId": 103,

    "sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",

    "dId": 494,

    "name": "Test3",

  },

  {

    "orgId": 103,

    "sequesnce": "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR",

    "dId": 494,

    "name": "Test3",

  },

  {

    "orgId": 103,

    "sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",

    "dId": 494,

    "name": "Test3",

  }

];


const sortedArr = array1.map(({ sequesnce }) => ({ sequesnce })).sort((a, b) => (b.sequesnce.localeCompare(a.sequesnce, undefined, { numeric: true, sensitivity: 'base'})));


const topCount = 3;

console.log(sortedArr.splice(0, topCount));


查看完整回答
反对 回复 2023-05-11
  • 3 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信