javascript - 通过字符串的多个位置从字符串生成合并数组
问题描述
例如,
const text = "APPLE ORANGE";
const text_position = [0,4,4,7,9];
const inserted_value = ["yo","wo","go","lo","zo"];
对于这个例子,我想创建一个这样的数组:
return ["yo","APPL","wo","go","E O","lo","RA","zo","NGE"];
我的代码:
我正在尝试通过字符串位置数组从给定字符串合并到一个数组中。给出了一个字符串和两个数组:
const content = "0123456789TEXT";
const footnote_position = [0, 1, 2, 2, 6]; // string positions
const footnote_value = ["ZERO", "ONE", "TWO", "TWO", "SIX"]; // inserted values
但是对于我的代码及以上给出content
的footnote_position
, 和footnote_value
, 算法必须输出如下:
["ZEROR","0","ONE","1","TWO","TWO","2345","SIX","67899TEXT"]
我的完整代码是:
const content = "0123456789TEXT";
const footnote_position = [0, 1, 2, 2, 6]; // must be sorted
const footnote_value = ["ZERO", "ONE", "TWO", "TWO", "SIX"];
const position_set = [...new Set(footnote_position)]; // must be sorted 1,2,6
const contentArray = [];
let textArray = [];
let prev = -1;
let count = footnote_position.length;
for (let index = 0; index < count + 1; index++) {
switch (index) {
case 0: // ok
var item = footnote_position[index];
if (item != 0) {
textArray.push(content.substring(0, item));
}
footnote_position.forEach((value, position) => {
if (value == item) {
textArray.push(footnote_value[position]);
}
})
prev = item;
break;
case length: // ok
textArray.push(content.substring(prev)); // <Text>
footnote_position.forEach((value, position) => {
if (value == item) textArray.push(footnote_value[position]);
})
break;
default: // not ok
var item = footnote_position[index];
textArray.push(content.substring(prev, item));
footnote_position.forEach((value, position) => {
if (value == item) textArray.push(footnote_value[position]);
})
prev = item;
break;
}
}
console.log(textArray);
不幸的是,我的输出不同如下:
["ZERO", "0", "ONE", "1", "TWO", "TWO", "", "TWO", "TWO", "2345", "SIX", "6789TEXT"]
什么地方出了错?对于这个问题,您有其他不同的算法解决方案吗?
另外,我真的不知道为什么case length:
工作。代码中没有定义变量length
。
解决方案
使用forEach
和slice
维护last
更新:修复最后一个元素的问题。很好的建议@mplungjan,谢谢。
const text = "APPLE ORANGE";
const text_position = [0, 4, 4, 7, 9];
const inserted_value = ["yo", "wo", "go", "lo", "zo"];
let last = 0;
const output = [];
text_position.forEach((index, i) => {
const value = text.slice(last, index);
if (value) {
output.push(value);
}
output.push(inserted_value[i]);
last = index;
});
if (last < text.length) output.push(text.slice(last));
console.log(output);
替代方式使用flatMap
const text = "APPLE ORANGE";
const text_position = [0, 4, 4, 7, 9];
const inserted_value = ["yo", "wo", "go", "lo", "zo"];
let last = 0;
const output = text_position.flatMap((index, i) => {
const output = [];
last < index && output.push(text.slice(last, index));
output.push(inserted_value[i]);
last = index;
(i === (text_position.length - 1)) && (last < text.length) && output.push(text.slice(last));
return output;
})
console.log(output);
推荐阅读
- python - 如何选择随机字符串的值和长度?
- javascript - 为什么在表单内的 onclick 事件上调用名为“submit()”的函数在 WebKit 中的行为不同?
- docker - 如何了解 prometeus exporter agent 是否在远程机器上导出指标
- c - 在C文件的开头插入文件中的行数的最佳方法是什么?
- express - 不知道如何让快速路由工作
- html - 在 v5 中打印 FullCalendar 实例
- python - 如何为 CNN 提供 tf.data.Dataset
- ios - AppTrackingTransparency... 对话框仅显示一次
- spring - Spring JPA @Query:在使用它之前检查参数是否为空 - 糟糕的休眠翻译?
- git - 如何将代码合并到 Github 的主分支?