首页 > 解决方案 > 通过字符串的多个位置从字符串生成合并数组

问题描述

例如,

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

但是对于我的代码及以上给出contentfootnote_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

标签: javascriptarraysalgorithmmerge

解决方案


使用forEachslice维护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);


推荐阅读