Javascript 删除特定对象后,在数组中的对象中设置新序列
我的目标: 我需要在sequenceIndex中有一个连续的数字序列,它是我对象中的一个值 所以当我移除一个特定的对象时,其他对象的序列索引当然不再是连续的了。正在对照特定值检查我移除的对象,以查看数组中是否有其他对象共享相同的值(第二个if语句)。如果是这样,那么应该有一个连续的新值集 结果是if语句中的迭代器对于所有被操作的对象总是相同的 由此:Javascript 删除特定对象后,在数组中的对象中设置新序列,javascript,reactjs,Javascript,Reactjs,我的目标: 我需要在sequenceIndex中有一个连续的数字序列,它是我对象中的一个值 所以当我移除一个特定的对象时,其他对象的序列索引当然不再是连续的了。正在对照特定值检查我移除的对象,以查看数组中是否有其他对象共享相同的值(第二个if语句)。如果是这样,那么应该有一个连续的新值集 结果是if语句中的迭代器对于所有被操作的对象总是相同的 由此: const objectsArray = [ { folder: "folderName", documents: [
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,我想得出以下结论(请参阅continuous sequenceIndex):
到目前为止,我的代码是:
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;
}
return {
...obj
};
});
return {
...file,
documents: newObjArray
};
}
return file;
});
我希望有人能指引我正确的方向。我还将始终感谢您对最佳实践的建议:)
致意您可以使用
过滤器
和映射
类似的功能
const arr=[{文件夹:“folderName”,文档:[{id:0,sequenceIndex:“0”,documentType:“字母”},{id:1,sequenceIndex:“1”,documentType:“字母”},{id:2,sequenceIndex:“2”,documentType:“字母”},{id:3,sequenceIndex:“3”,documentType:“字母”}];
让getInSequence=(filterId)=>{
返回arr[0]。documents.filter(({id})=>!filterId.includes(id))
.map((v,i)=>({…v,sequenceIndex:i}))
}
log(getInSequence([1,2])
您可以使用filter
和map
类似的方法
const arr=[{文件夹:“folderName”,文档:[{id:0,sequenceIndex:“0”,documentType:“字母”},{id:1,sequenceIndex:“1”,documentType:“字母”},{id:2,sequenceIndex:“2”,documentType:“字母”},{id:3,sequenceIndex:“3”,documentType:“字母”}];
让getInSequence=(filterId)=>{
返回arr[0]。documents.filter(({id})=>!filterId.includes(id))
.map((v,i)=>({…v,sequenceIndex:i}))
}
log(getInSequence([1,2])
我现在用来解决这个问题的解决方案是:
let count = 0;
const newObject = file.documents.map(obj => {
if (obj.documentType === firstDocument.documentType) {
count++;
return { ...obj, sequenceIndex: count - 1 };
}
return obj;
});
提供的两个答案都无法处理由于文档类型不同而不感兴趣的对象,因此他们放弃了该对象。使用这个解决方案,我检查最后一个元素,如果最后一个元素是相同的documentType,则增加计数 我现在用来解决这个问题的方法是:
let count = 0;
const newObject = file.documents.map(obj => {
if (obj.documentType === firstDocument.documentType) {
count++;
return { ...obj, sequenceIndex: count - 1 };
}
return obj;
});
提供的两个答案都无法处理由于文档类型不同而不感兴趣的对象,因此他们放弃了该对象。使用这个解决方案,我检查最后一个元素,如果最后一个元素是相同的documentType,则增加计数 如评论所述:
这是一个典型的例子,其中.filter().map()将非常有用。过滤数据,然后使用.map((o,i)=>({…obj,sequenceIndex:i+1})) 以下是示例:
const objectsArray=[{
文件夹:“folderName”,
文件:[{
id:0,
sequenceIndex:“0”,
文档类型:“字母”
},
{
id:1,
顺序索引:“1”,
文档类型:“字母”
},
{
id:2,
序列索引:“2”,
文档类型:“字母”
},
{
id:3,
顺序索引:“3”,
文档类型:“字母”
}
]
}];
常量ignoreIds=[1,2]
const updatedocs=objectsArray[0]。文档
.过滤器(({
身份证件
})=>!IgnoreID.includes(id))
.map((doc,index)=>({…doc,
sequenceIndex:index
}));
console.log(updatedDocs)
如注释所示:
这是一个典型的例子,其中.filter().map()将非常有用。过滤数据,然后使用.map((o,i)=>({…obj,sequenceIndex:i+1})) 以下是示例:
const objectsArray=[{
文件夹:“folderName”,
文件:[{
id:0,
sequenceIndex:“0”,
文档类型:“字母”
},
{
id:1,
顺序索引:“1”,
文档类型:“字母”
},
{
id:2,
序列索引:“2”,
文档类型:“字母”
},
{
id:3,
顺序索引:“3”,
文档类型:“字母”
}
]
}];
常量ignoreIds=[1,2]
const updatedocs=objectsArray[0]。文档
.过滤器(({
身份证件
})=>!IgnoreID.includes(id))
.map((doc,index)=>({…doc,
sequenceIndex:index
}));
console.log(updatedDocs)
这是一个经典案例,其中.filter().map()
将非常有用。过滤数据,然后使用.map((o,i)=>({…obj,sequenceIndex:i+1}))
这是.filter().map()非常有用的典型情况。过滤数据,然后使用.map((o,i)=>({…obj,sequenceIndex:i+1}))
hmm它还没有完全起作用。我得到一个ts错误:类型为的参数(acc:any[],obj:any)=>any[]”不可分配给类型为
的参数应该如何定义IDocument答案中的类型是肤浅的。你必须使用你的类型。好的,谢谢!如果我有其他类型的信件呢。也会从数组中抛出。可能是我有各种不同的类型。到目前为止,我还没有达到预期的效果。我得到一个ts错误:类型为的参数(acc:any[],obj:any)=>any[]”不可分配给类型为
的参数应该如何定义IDocument答案中的类型是肤浅的。你必须使用你的类型。好的,谢谢!如果我有其他类型的信件呢。也会从数组中抛出。可能是我有各种不同的类型。到目前为止,我还没有达到预期的结果。