第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何定位數(shù)組中的對(duì)象子集并將屬性更改為每個(gè)對(duì)象的遞增數(shù)字?

如何定位數(shù)組中的對(duì)象子集并將屬性更改為每個(gè)對(duì)象的遞增數(shù)字?

LEATH 2022-10-08 10:23:12
我正在努力對(duì)一組對(duì)象進(jìn)行相當(dāng)復(fù)雜的操作。首先這是一個(gè)示例數(shù)組:[  {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}]所以我想盡可能高效地按順序進(jìn)行以下操作。按 lineNum 對(duì)數(shù)組進(jìn)行排序,然后按項(xiàng)目升序排序。然后我想修改每個(gè)對(duì)象,lineNum === 0使 linePosition 從 0 開(kāi)始遞增。所以第一個(gè)匹配對(duì)象的 linePosition 為 0,下一個(gè)是 1,然后是 2,等等。結(jié)果數(shù)組如下所示:[  {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}]
查看完整描述

2 回答

?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

Array.prototype.sort()

let arr = getData();


arr.sort(function(a, b) {

  if( a.lineNum === b.lineNum ) {

    return a.item - b.item;

  } else {

    return a.lineNum - b.lineNum;

  }

});

// return a.lineNum === b.lineNum ? a.item - b.item : a.lineNum - b.lineNum;


for( let i = 0; i < arr.length; i++ ) {

  if( arr[i].lineNum > 0 ) break;

  arr[i].linePosition = i;

}

// Used classic loop, to break it and avoid unnecessary iterations.


console.log( JSON.stringify(arr).replace(/\},/g, "},\n") );


/***/

function getData() {

  return [

    {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},

  ];

}

.as-console-wrapper { max-height: 100vh !important; }


查看完整回答
反對(duì) 回復(fù) 2022-10-08
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以使用 Javascript.sort()函數(shù)完成上述任務(wù)。它可以采用可選的比較函數(shù)參數(shù),該參數(shù)指定定義排序順序的函數(shù)。


let arr = [

  {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}

];


//sorting the given array, first based on lineNum, then by item

arr.sort(function(a,b){

  if(a.lineNum !== b.lineNum)

    return a.lineNum - b.lineNum;

  return a.item - b.item;

});


// modify each object where lineNum===0, linePosition starting from 0

// since we have sorted, break when first lineNum > 0

let linePos = 0;

for( let i = 0; i < arr.length; i++ ) {

  if( arr[i].lineNum > 0 ) 

    break;

  if(arr[i].lineNum === 0)

    arr[i].linePosition = linePos++;

}


//   console.log(arr); //<-- This prints as array


console.log( JSON.stringify(arr).replace(/\},/g, "},\n") ); // <-- This prints your mentioned format


查看完整回答
反對(duì) 回復(fù) 2022-10-08
  • 2 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)