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

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

刪除特定對(duì)象后在對(duì)象中從數(shù)組中設(shè)置新序列

刪除特定對(duì)象后在對(duì)象中從數(shù)組中設(shè)置新序列

Qyouu 2021-10-29 16:33:25
我的目標(biāo): 我需要在 sequenceIndex 中有一個(gè)連續(xù)的數(shù)字序列,它是我對(duì)象中的一個(gè)值。因此,當(dāng)我刪除特定對(duì)象時(shí),其他對(duì)象的序列索引當(dāng)然不再連續(xù)了。正在根據(jù)特定值檢查我刪除的對(duì)象,以查看數(shù)組中是否還有其他對(duì)象共享相同的值(第二個(gè) if 語句)。如果是這樣,那么應(yīng)該有一個(gè)連續(xù)的新值集。輸出是 if 語句中的迭代器對(duì)于所有操作的對(duì)象始終相同。由此:const objectsArray = [  {    folder: "folderName",    documents: [      {        id: 0,        sequenceIndex: "0",        documentType: "letter"      },      {        id: 1,        sequenceIndex: "1",        documentType: "letter"      },      {        id: 2,        sequenceIndex: "2",        documentType: "letter"      },      {        id: 3,        sequenceIndex: "3",        documentType: "letter"      }    ]  }];通過刪除 id 1 和 2,我想解決這個(gè)問題(請(qǐng)參閱連續(xù)序列索引):const desiredObjectsArray = [  {    folder: "folderName",    documents: [      {        id: 0,        sequenceIndex: "0",        documentType: "letter"      },      {        id: 3,        sequenceIndex: "1",        documentType: "letter"      }    ]  }];到目前為止我的代碼:case ActionType.RemoveDocumentInSpecificFolder:        return state.map(file => {          // if in the correct folder remove the object with the delivered id          if (file.folder=== folder) {            remove(file.documents, {              id: action.payload.documents[0].id            });            // create newObjArray from objects which share a specific value and replace the sequence index by new value            const newObjArray = file.documents.map((obj: any) => {              // if the object has the specific value create new object with new sequenceIndex              if (obj.documentType === action.payload.documents[0].documentType) {                //poor attempt to create a sequence                let i = 0;                const correctedSequenceDocObject = { ...obj, sequenceIndex: i };                i++;                return correctedSequenceDocObject;              }我希望有人能指導(dǎo)我朝著正確的方向前進(jìn)。我也將永遠(yuǎn)感謝最佳實(shí)踐的建議:)
查看完整描述

3 回答

?
手掌心

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

正如評(píng)論:

這是 .filter().map() 有用的經(jīng)典案例。過濾數(shù)據(jù),然后使用 .map((o, i) => ({ ...obj, sequenceIndex: i+1 }) )

以下是示例:

const objectsArray = [{

  folder: "folderName",

  documents: [{

      id: 0,

      sequenceIndex: "0",

      documentType: "letter"

    },

    {

      id: 1,

      sequenceIndex: "1",

      documentType: "letter"

    },

    {

      id: 2,

      sequenceIndex: "2",

      documentType: "letter"

    },

    {

      id: 3,

      sequenceIndex: "3",

      documentType: "letter"

    }

  ]

}];

const ignoreIds = [1, 2]

const updatedDocs = objectsArray[0].documents

  .filter(({

    id

  }) => !ignoreIds.includes(id))

  .map((doc, index) => ({ ...doc,

    sequenceIndex: index

  }));


console.log(updatedDocs)

現(xiàn)在讓我們介紹您的嘗試


const newObjArray = file.documents.map((obj: any) => {

  // For all the unmatching objects, you will have undefined as object as you are using `.map`

  // This will make you `newObjArray: Array<IDocument | undefined>` which can break your code.

  if (obj.documentType === action.payload.documents[0].documentType) {


    // This will set it as 0 in every iteration making i as 0 always.

    let i = 0;

    const correctedSequenceDocObject = { ...obj, sequenceIndex: i };

    i++;

    return correctedSequenceDocObject;


  }

  return { ...obj };

});

單循環(huán)的替代:


主意:

使用創(chuàng)建一個(gè)循環(huán)Array.reduce并將一個(gè)空白數(shù)組作為列表傳遞給它。

添加一個(gè)檢查并在其中將值推送到此列表。

對(duì)于sequenceIndex,獲取最后一個(gè)元素并獲取其sequenceIndex. 添加一個(gè)并重新設(shè)置。

const newObjArray = file.documents.reduce((acc: Array<IDocument>, obj: any) => {

  if (obj.documentType === action.payload.documents[0].documentType) {

    const sequenceIndex: number = (!!acc[acc.length - 1] ? acc[acc.length - 1].sequenceIndex : 1) + 1;

    acc.push({ ...obj, sequenceIndex });

  }

  return acc;

});


查看完整回答
反對(duì) 回復(fù) 2021-10-29
?
月關(guān)寶盒

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

你可以使用filter和map這樣的東西


const arr = [{folder: "folderName",documents: [{id: 0,sequenceIndex: "0",documentType: "letter"},{id: 1,sequenceIndex: "1",documentType: "letter"},{id: 2,sequenceIndex: "2",documentType: "letter"},{id: 3,sequenceIndex: "3",documentType: "letter"}]}];


let getInSequence = (filterId) => {

  return arr[0].documents.filter(({ id }) => !filterId.includes(id))

               .map((v, i) => ({ ...v, sequenceIndex: i }))

}


console.log(getInSequence([1, 2]))


查看完整回答
反對(duì) 回復(fù) 2021-10-29
?
波斯汪

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

我現(xiàn)在用來解決這個(gè)問題的解決方案是:


            let count = 0;

            const newObject = file.documents.map(obj => {

              if (obj.documentType === firstDocument.documentType) {

                count++;

                return { ...obj, sequenceIndex: count - 1 };

              }

              return obj;

            });

由于不同的 documentType,提供的兩個(gè)答案都無法處理不感興趣的對(duì)象,因此他們刪除了對(duì)象。使用此解決方案,我正在檢查最后一個(gè)元素并增加計(jì)數(shù),如果最后一個(gè)元素是相同的 documentType。


查看完整回答
反對(duì) 回復(fù) 2021-10-29
  • 3 回答
  • 0 關(guān)注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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