亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

刪除特定對象后在對象中從數組中設置新序列

刪除特定對象后在對象中從數組中設置新序列

Qyouu 2021-10-29 16:33:25
我的目標: 我需要在 sequenceIndex 中有一個連續的數字序列,它是我對象中的一個值。因此,當我刪除特定對象時,其他對象的序列索引當然不再連續了。正在根據特定值檢查我刪除的對象,以查看數組中是否還有其他對象共享相同的值(第二個 if 語句)。如果是這樣,那么應該有一個連續的新值集。輸出是 if 語句中的迭代器對于所有操作的對象始終相同。由此: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,我想解決這個問題(請參閱連續序列索引):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;              }我希望有人能指導我朝著正確的方向前進。我也將永遠感謝最佳實踐的建議:)
查看完整描述

3 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

正如評論:

這是 .filter().map() 有用的經典案例。過濾數據,然后使用 .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)

現在讓我們介紹您的嘗試


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

});

單循環的替代:


主意:

使用創建一個循環Array.reduce并將一個空白數組作為列表傳遞給它。

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

對于sequenceIndex,獲取最后一個元素并獲取其sequenceIndex. 添加一個并重新設置。

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;

});


查看完整回答
反對 回復 2021-10-29
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

你可以使用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]))


查看完整回答
反對 回復 2021-10-29
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

我現在用來解決這個問題的解決方案是:


            let count = 0;

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

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

                count++;

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

              }

              return obj;

            });

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


查看完整回答
反對 回復 2021-10-29
  • 3 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號