删除并更新 javascript 中对象数组中的重叠索引
Remove and update overlapping indexes in array of object in javascript
我有一个具有重叠索引的对象数组。
[
//1st object
{
start: 3,
end: 10
},
//2nd object
{
start: 5,
end: 8
},
//3rd object
{
start: 11,
end: 30
},
//4th object
{
start: 0,
end: 4
}
]
我想通过修改和删除一些重叠对象来创建数组新数组,即
case1:- 如果任何对象完全落在一个区间内则移除,即第二个对象可以被移除,因为它落在第一个对象中
case2:- 如果任何对象部分落在区间内,我想更新索引。这样所有的对象都可以不同
最终结果如下所示。
[
//I increased 1st object by 2 as 4th object's end index is 4. I increased (end of 4th - start of 1st) + 1
{
start: 5,
end: 12
},
//I removed 2nd object
// I changed the 3rd object by 2 because it's overlapping with the newly created `{start:5, end:12}` object
{
start: 13,
end: 32
},
// No change in 4th object.
{
start: 0,
end: 4
}
]
任何建议我如何解决这个问题。
我尝试了以下方法来修改数组。
const updateArray = arr => {
let sortArr = arr.sort((a,b)=> a.start - b.start || a.end - b.end);
let newArr = sortArr.reduce((r,a) =>{
let obj = r[r.length - 1] || {};
if (obj.start <= a.start && a.start <= obj.end) {
if (obj.end < a.end) {
a.start = obj.end + 1;
} else {
obj.start = a.end + 1;
}
}
return r.concat(a);
},[]);
return newArr;
}
我假设排序输出会很好(因为您使用的是一个)。如果是这样,那么下面的脚本将解决它。
脚本:
function modifyArray(array) {
var prevEnd;
array = array.sort((x,y) => x.start - y.start);
for(i = 0; i < array.length; i++) {
if(!i) {
prevEnd = array[i].end;
continue;
}
if(array[i].start < prevEnd && array[i].end > prevEnd) {
diff = prevEnd + 1 - array[i].start;
array[i].start += diff;
array[i].end += diff;
prevEnd = array[i].end;
}
else if(array[i].start > prevEnd)
continue;
else
array.splice(i--, 1);
}
return array;
}
function test() {
let array1 = [ { start: 3, end: 10 },
{ start: 5, end: 8 },
{ start: 11, end: 30 },
{ start: 0, end: 4 } ];
let array2 = [{start: 10, end: 12}, {start:17, end: 19}]
console.log(modifyArray(array1))
console.log(modifyArray(array2))
}
输出:
我有一个具有重叠索引的对象数组。
[
//1st object
{
start: 3,
end: 10
},
//2nd object
{
start: 5,
end: 8
},
//3rd object
{
start: 11,
end: 30
},
//4th object
{
start: 0,
end: 4
}
]
我想通过修改和删除一些重叠对象来创建数组新数组,即 case1:- 如果任何对象完全落在一个区间内则移除,即第二个对象可以被移除,因为它落在第一个对象中 case2:- 如果任何对象部分落在区间内,我想更新索引。这样所有的对象都可以不同
最终结果如下所示。
[
//I increased 1st object by 2 as 4th object's end index is 4. I increased (end of 4th - start of 1st) + 1
{
start: 5,
end: 12
},
//I removed 2nd object
// I changed the 3rd object by 2 because it's overlapping with the newly created `{start:5, end:12}` object
{
start: 13,
end: 32
},
// No change in 4th object.
{
start: 0,
end: 4
}
]
任何建议我如何解决这个问题。 我尝试了以下方法来修改数组。
const updateArray = arr => {
let sortArr = arr.sort((a,b)=> a.start - b.start || a.end - b.end);
let newArr = sortArr.reduce((r,a) =>{
let obj = r[r.length - 1] || {};
if (obj.start <= a.start && a.start <= obj.end) {
if (obj.end < a.end) {
a.start = obj.end + 1;
} else {
obj.start = a.end + 1;
}
}
return r.concat(a);
},[]);
return newArr;
}
我假设排序输出会很好(因为您使用的是一个)。如果是这样,那么下面的脚本将解决它。
脚本:
function modifyArray(array) {
var prevEnd;
array = array.sort((x,y) => x.start - y.start);
for(i = 0; i < array.length; i++) {
if(!i) {
prevEnd = array[i].end;
continue;
}
if(array[i].start < prevEnd && array[i].end > prevEnd) {
diff = prevEnd + 1 - array[i].start;
array[i].start += diff;
array[i].end += diff;
prevEnd = array[i].end;
}
else if(array[i].start > prevEnd)
continue;
else
array.splice(i--, 1);
}
return array;
}
function test() {
let array1 = [ { start: 3, end: 10 },
{ start: 5, end: 8 },
{ start: 11, end: 30 },
{ start: 0, end: 4 } ];
let array2 = [{start: 10, end: 12}, {start:17, end: 19}]
console.log(modifyArray(array1))
console.log(modifyArray(array2))
}