给定一个2D数组,我正在寻找一种优雅和高效的方法来划分一个一维数组的words,给定一个开始索引和结束索引。
// pass this in as an arg
var trim = [
[3, 4], // remove two words at index 3 and 4
[9, 10] // remove two words at index 9 and 10
]; // use this to reformat
var words = [
{ word: "hello", st: 0 },
{ word: "stack-overflow", st: 0.5 },
{ word: "lets", st: 1 },
{ word: "remove", st: 1.5 },
{ word: "some", st: 2 },
{ word: "words", st: 2.5 },
{ word: "efficiently", st: 3 },
{ word: "lets", st: 3.5 },
{ word: "do", st: 4 },
{ word: "it", st: 4.5 },
{ word: "yay", st: 5 }
];
// this is the result I am looking for
var result = [
[
{ word: "hello", st: 0 },
{ word: "stack-overflow", st: 0.5 },
{ word: "lets", st: 1 }
],
[
{ word: "words", st: 2.5 },
{ word: "efficiently", st: 3 },
{ word: "lets", st: 3.5 },
{ word: "do", st: 4 }
]
];这是我所能做的最好的了,丢失了2D数组的返回。
words.reduce((acc, curr, i) => {
const wordBetween = trim.some(t => {
return t[0] <= i && t[1] >= i
});
console.log({wordBetween, curr})
if (wordBetween) {
return acc;
}
return [...acc, curr]
}, [])发布于 2022-12-01 03:49:23
纯粹的优雅!还有最棒的表演!撇开笑话不说,这应该能传达这个想法。
let start = 0;
let result = [];
for (let [end, newStart] of trim) {
result.push(words.slice(start, end));
start = newStart + 1;
}
result.push(words.slice(start, words.length));请注意,2拆分会导致三个块(n个拆分给n+1块),因此在开始或结束时可能会出现一个空数组。这在string.split中有优先权。
发布于 2022-12-01 03:26:49
这里有一种方法可以过滤数组,删除一些指定的索引.
const arrayWithoutIndexes = (array, indexes) => {
return array.filter((el, i) => !indexes.includes(i));
};
var words = [
{ word: "hello", st: 0 },
{ word: "stack-overflow", st: 0.5 },
{ word: "lets", st: 1 },
{ word: "remove", st: 1.5 },
{ word: "some", st: 2 },
{ word: "words", st: 2.5 },
{ word: "efficiently", st: 3 },
{ word: "lets", st: 3.5 },
{ word: "do", st: 4 },
{ word: "it", st: 4.5 },
{ word: "yay", st: 5 }
];
console.log(arrayWithoutIndexes(words, [1,4]));
编辑:要处理几组索引,为每组索引生成一组新编辑的单词,只需将该函数映射到索引输入上.
const arrayWithoutIndexes = (array, indexes) => {
// the map here just copies the word, so the result has new word objects
return array.filter((el, i) => !indexes.includes(i)).map(o => Object.assign({}, o));
};
var words = [
{ word: "hello", st: 0 },
{ word: "stack-overflow", st: 0.5 },
{ word: "lets", st: 1 },
{ word: "remove", st: 1.5 },
{ word: "some", st: 2 },
{ word: "words", st: 2.5 },
{ word: "efficiently", st: 3 },
{ word: "lets", st: 3.5 },
{ word: "do", st: 4 },
{ word: "it", st: 4.5 },
{ word: "yay", st: 5 }
];
let removeThese = [
[2, 3],
[4, 5]
];
let result = removeThese.map(indexes => arrayWithoutIndexes(words, indexes));
console.log(result)
发布于 2022-12-01 03:44:31
我们可以利用JSON.stringify来分割对象。
const splitArrayBasedOnIndexes = (arr = [], indexToRemove = []) => {
indexToRemove = indexToRemove.flat(Infinity);
let result = JSON.stringify(arr.map((w, i) => {
if (indexToRemove.includes(i)) return null;
return w
}))
result = result.substring(1, result.lastIndexOf("]")).split("null").reduce((p, c) => {
if (c.substring(c.indexOf("{"), c.lastIndexOf("}") + 1))
p.push(JSON.parse("[" + c.substring(c.indexOf("{"), c.lastIndexOf("}") + 1) + "]"))
return p
}, []);
return result;
}
const words = [
{ word: "hello", st: 0 },
{ word: "stack-overflow", st: 0.5 },
{ word: "lets", st: 1 },
{ word: "remove", st: 1.5 },
{ word: "some", st: 2 },
{ word: "words", st: 2.5 },
{ word: "efficiently", st: 3 },
{ word: "lets", st: 3.5 },
{ word: "do", st: 4 },
{ word: "it", st: 4.5 },
{ word: "yay", st: 5 }
];
const trim = [
[3, 4],
[9, 10]
];
console.log(splitArrayBasedOnIndexes(words, trim));
https://stackoverflow.com/questions/74636557
复制相似问题