删除特定对象后在对象中从数组中设置新序列

我的目标: 我需要在 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;


              }


我希望有人能指导我朝着正确的方向前进。我也将永远感谢最佳实践的建议:)


Qyouu
浏览 167回答 3
3回答

手掌心

正如评论:这是 .filter().map() 有用的经典案例。过滤数据,然后使用 .map((o, i) => ({ ...obj, sequenceIndex: i+1 }) )以下是示例:const objectsArray = [{&nbsp; folder: "folderName",&nbsp; documents: [{&nbsp; &nbsp; &nbsp; id: 0,&nbsp; &nbsp; &nbsp; sequenceIndex: "0",&nbsp; &nbsp; &nbsp; documentType: "letter"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; id: 1,&nbsp; &nbsp; &nbsp; sequenceIndex: "1",&nbsp; &nbsp; &nbsp; documentType: "letter"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; id: 2,&nbsp; &nbsp; &nbsp; sequenceIndex: "2",&nbsp; &nbsp; &nbsp; documentType: "letter"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; id: 3,&nbsp; &nbsp; &nbsp; sequenceIndex: "3",&nbsp; &nbsp; &nbsp; documentType: "letter"&nbsp; &nbsp; }&nbsp; ]}];const ignoreIds = [1, 2]const updatedDocs = objectsArray[0].documents&nbsp; .filter(({&nbsp; &nbsp; id&nbsp; }) => !ignoreIds.includes(id))&nbsp; .map((doc, index) => ({ ...doc,&nbsp; &nbsp; sequenceIndex: index&nbsp; }));console.log(updatedDocs)现在让我们介绍您的尝试const newObjArray = file.documents.map((obj: any) => {&nbsp; // For all the unmatching objects, you will have undefined as object as you are using `.map`&nbsp; // This will make you `newObjArray: Array<IDocument | undefined>` which can break your code.&nbsp; if (obj.documentType === action.payload.documents[0].documentType) {&nbsp; &nbsp; // This will set it as 0 in every iteration making i as 0 always.&nbsp; &nbsp; let i = 0;&nbsp; &nbsp; const correctedSequenceDocObject = { ...obj, sequenceIndex: i };&nbsp; &nbsp; i++;&nbsp; &nbsp; return correctedSequenceDocObject;&nbsp; }&nbsp; return { ...obj };});单循环的替代:主意:使用创建一个循环Array.reduce并将一个空白数组作为列表传递给它。添加一个检查并在其中将值推送到此列表。对于sequenceIndex,获取最后一个元素并获取其sequenceIndex. 添加一个并重新设置。const newObjArray = file.documents.reduce((acc: Array<IDocument>, obj: any) => {&nbsp; if (obj.documentType === action.payload.documents[0].documentType) {&nbsp; &nbsp; const sequenceIndex: number = (!!acc[acc.length - 1] ? acc[acc.length - 1].sequenceIndex : 1) + 1;&nbsp; &nbsp; acc.push({ ...obj, sequenceIndex });&nbsp; }&nbsp; return acc;});

月关宝盒

你可以使用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) => {&nbsp; return arr[0].documents.filter(({ id }) => !filterId.includes(id))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map((v, i) => ({ ...v, sequenceIndex: i }))}console.log(getInSequence([1, 2]))

波斯汪

我现在用来解决这个问题的解决方案是:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let count = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const newObject = file.documents.map(obj => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (obj.documentType === firstDocument.documentType) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return { ...obj, sequenceIndex: count - 1 };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return obj;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });由于不同的 documentType,提供的两个答案都无法处理不感兴趣的对象,因此他们删除了对象。使用此解决方案,我正在检查最后一个元素并增加计数,如果最后一个元素是相同的 documentType。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript