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

我有数组,其中有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",

},

此代码返回单个数组而不是三个,并且不确定在所有情况下此逻辑是否有效。


翻翻过去那场雪
浏览 124回答 3
3回答

BIG阳

您可以排序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; }

慕慕森

如果序列较短且前导值较高,则需要在排序前扩展元组 - 这不适用于 localeCompareconst maxLen = 4;const expandTuple = tpl => {&nbsp; tpl = tpl.split('.');&nbsp; while (tpl.length < maxLen) tpl.unshift("000");&nbsp; return tpl.map(x => x.padStart(3, "0")).join('.');};const array1 = [{ "orgId": 101, "dId": 494, "name": "Test1",&nbsp; &nbsp; "sequence": "6.408.06.0 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR" },{"orgId": 102,"dId": 442,"name": "Test2",&nbsp; &nbsp; "sequence": "9.80.709.5 Sequence Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",},{"orgId": 103,&nbsp; &nbsp; "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,&nbsp; &nbsp; "sequence": "8.208.409.0 Sequence Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR","dId": 494,"name": "Test3",},{"orgId": 103,&nbsp; &nbsp; "sequence": "9.408.90.3 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR","dId": 494,&nbsp; &nbsp;"name": "Test3",&nbsp; }]const three = array1.sort((a,b) => {&nbsp; const aTub = a.sequence.split(" ")[0];&nbsp; const bTub = b.sequence.split(" ")[0];&nbsp; if (expandTuple(aTub)<expandTuple(bTub)) return 1&nbsp; if (expandTuple(aTub)>expandTuple(bTub)) return -1&nbsp; return 0;}).slice(0,3)console.log(three)// ---- compare other solution which fails on the shorter sequenceconsole.log(array1&nbsp; .sort((a, b) => b.sequence.localeCompare(a.sequence, undefined, { numeric: true, sensitivity: 'base' }))&nbsp; .slice(0, 3))

肥皂起泡泡

首先,使用Array.map,您可以仅生成项目对象数组sequesne。并使用Array.sort,您可以按降序对数组进行排序。并使用Array.splice,您可以从排序数组中提取前 3 项。const array1 = [{&nbsp; &nbsp; "orgId": 101,&nbsp; &nbsp; "dId": 494,&nbsp; &nbsp; "name": "Test1",&nbsp; &nbsp; "sequesnce": "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR"&nbsp; },&nbsp; {&nbsp; &nbsp; "orgId": 102,&nbsp; &nbsp; "dId": 442,&nbsp; &nbsp; "name": "Test2",&nbsp; &nbsp; "sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",&nbsp; },&nbsp; {&nbsp; &nbsp; "orgId": 103,&nbsp; &nbsp; "sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",&nbsp; &nbsp; "dId": 494,&nbsp; &nbsp; "name": "Test3",&nbsp; },&nbsp; {&nbsp; &nbsp; "orgId": 103,&nbsp; &nbsp; "sequesnce": "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR",&nbsp; &nbsp; "dId": 494,&nbsp; &nbsp; "name": "Test3",&nbsp; },&nbsp; {&nbsp; &nbsp; "orgId": 103,&nbsp; &nbsp; "sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",&nbsp; &nbsp; "dId": 494,&nbsp; &nbsp; "name": "Test3",&nbsp; }];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));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript