如何定位数组中的对象子集并将属性更改为每个对象的递增数字?

我正在努力对一组对象进行相当复杂的操作。首先这是一个示例数组:


[

  {id: 11, item: 132, lineNum: 4, linePosition: 5}

  {id: 2, item: 124, lineNum: 0, linePosition: 2}

  {id: 9, item: 130, lineNum: 0, linePosition: 5}

  {id: 12, item: 133, lineNum: 1, linePosition: 3}

  {id: 15, item: 136, lineNum: 3, linePosition: 2}

  {id: 13, item: 134, lineNum: 0, linePosition: 4}

  {id: 10, item: 131, lineNum: 2, linePosition: 1}

  {id: 8, item: 129, lineNum: 3, linePosition: 3}

  {id: 1, item: 123, lineNum: 0, linePosition: 5}

  {id: 3, item: 125, lineNum: 1, linePosition: 7}

  {id: 5, item: 127, lineNum: 0, linePosition: 9}

  {id: 7, item: 128, lineNum: 1, linePosition: 2}

  {id: 4, item: 126, lineNum: 0, linePosition: 4}

  {id: 14, item: 135, lineNum: 3, linePosition: 9}

]

所以我想尽可能高效地按顺序进行以下操作。按 lineNum 对数组进行排序,然后按项目升序排序。然后我想修改每个对象,lineNum === 0使 linePosition 从 0 开始递增。所以第一个匹配对象的 linePosition 为 0,下一个是 1,然后是 2,等等。


结果数组如下所示:


[

  {id: 1, item: 123, lineNum: 0, linePosition: 0}

  {id: 2, item: 124, lineNum: 0, linePosition: 1}

  {id: 4, item: 126, lineNum: 0, linePosition: 2}

  {id: 5, item: 127, lineNum: 0, linePosition: 3}

  {id: 9, item: 130, lineNum: 0, linePosition: 4}

  {id: 13, item: 134, lineNum: 0, linePosition: 5}

  {id: 3, item: 125, lineNum: 1, linePosition: 7}

  {id: 7, item: 128, lineNum: 1, linePosition: 2}

  {id: 12, item: 133, lineNum: 1, linePosition: 3}

  {id: 10, item: 131, lineNum: 2, linePosition: 1}

  {id: 8, item: 129, lineNum: 3, linePosition: 3}

  {id: 14, item: 135, lineNum: 3, linePosition: 9}

  {id: 15, item: 136, lineNum: 3, linePosition: 2}

  {id: 11, item: 132, lineNum: 4, linePosition: 5}

]


LEATH
浏览 138回答 2
2回答

潇湘沐

Array.prototype.sort()let arr = getData();arr.sort(function(a, b) {&nbsp; if( a.lineNum === b.lineNum ) {&nbsp; &nbsp; return a.item - b.item;&nbsp; } else {&nbsp; &nbsp; return a.lineNum - b.lineNum;&nbsp; }});// return a.lineNum === b.lineNum ? a.item - b.item : a.lineNum - b.lineNum;for( let i = 0; i < arr.length; i++ ) {&nbsp; if( arr[i].lineNum > 0 ) break;&nbsp; arr[i].linePosition = i;}// Used classic loop, to break it and avoid unnecessary iterations.console.log( JSON.stringify(arr).replace(/\},/g, "},\n") );/***/function getData() {&nbsp; return [&nbsp; &nbsp; {id: 11, item: 132, lineNum: 4, linePosition: 5},&nbsp; &nbsp; {id: 2, item: 124, lineNum: 0, linePosition: 2},&nbsp; &nbsp; {id: 9, item: 130, lineNum: 0, linePosition: 5},&nbsp; &nbsp; {id: 12, item: 133, lineNum: 1, linePosition: 3},&nbsp; &nbsp; {id: 15, item: 136, lineNum: 3, linePosition: 2},&nbsp; &nbsp; {id: 13, item: 134, lineNum: 0, linePosition: 4},&nbsp; &nbsp; {id: 10, item: 131, lineNum: 2, linePosition: 1},&nbsp; &nbsp; {id: 8, item: 129, lineNum: 3, linePosition: 3},&nbsp; &nbsp; {id: 1, item: 123, lineNum: 0, linePosition: 5},&nbsp; &nbsp; {id: 3, item: 125, lineNum: 1, linePosition: 7},&nbsp; &nbsp; {id: 5, item: 127, lineNum: 0, linePosition: 9},&nbsp; &nbsp; {id: 7, item: 128, lineNum: 1, linePosition: 2},&nbsp; &nbsp; {id: 4, item: 126, lineNum: 0, linePosition: 4},&nbsp; &nbsp; {id: 14, item: 135, lineNum: 3, linePosition: 9},&nbsp; ];}.as-console-wrapper { max-height: 100vh !important; }

ABOUTYOU

您可以使用 Javascript.sort()函数完成上述任务。它可以采用可选的比较函数参数,该参数指定定义排序顺序的函数。let arr = [&nbsp; {id: 11, item: 132, lineNum: 4, linePosition: 5},&nbsp; {id: 2, item: 124, lineNum: 0, linePosition: 2},&nbsp; {id: 9, item: 130, lineNum: 0, linePosition: 5},&nbsp; {id: 12, item: 133, lineNum: 1, linePosition: 3},&nbsp; {id: 15, item: 136, lineNum: 3, linePosition: 2},&nbsp; {id: 13, item: 134, lineNum: 0, linePosition: 4},&nbsp; {id: 10, item: 131, lineNum: 2, linePosition: 1},&nbsp; {id: 8, item: 129, lineNum: 3, linePosition: 3},&nbsp; {id: 1, item: 123, lineNum: 0, linePosition: 5},&nbsp; {id: 3, item: 125, lineNum: 1, linePosition: 7},&nbsp; {id: 5, item: 127, lineNum: 0, linePosition: 9},&nbsp; {id: 7, item: 128, lineNum: 1, linePosition: 2},&nbsp; {id: 4, item: 126, lineNum: 0, linePosition: 4},&nbsp; {id: 14, item: 135, lineNum: 3, linePosition: 9}];//sorting the given array, first based on lineNum, then by itemarr.sort(function(a,b){&nbsp; if(a.lineNum !== b.lineNum)&nbsp; &nbsp; return a.lineNum - b.lineNum;&nbsp; return a.item - b.item;});// modify each object where lineNum===0, linePosition starting from 0// since we have sorted, break when first lineNum > 0let linePos = 0;for( let i = 0; i < arr.length; i++ ) {&nbsp; if( arr[i].lineNum > 0 )&nbsp;&nbsp; &nbsp; break;&nbsp; if(arr[i].lineNum === 0)&nbsp; &nbsp; arr[i].linePosition = linePos++;}//&nbsp; &nbsp;console.log(arr); //<-- This prints as arrayconsole.log( JSON.stringify(arr).replace(/\},/g, "},\n") ); // <-- This prints your mentioned format
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript