通过字符串的多个位置从字符串生成合并数组

问题描述

例如,

const text = "APPLE ORANGE";
const text_position = [0,4,7,9];
const inserted_value = ["yo","wo","go","lo","zo"];

对于这个例子,我想创建一个像这样的数组:

return ["yo","APPL","E O","RA","zo","NGE"];

Fianal Result

我的代码

我正在尝试通过字符串位置数组将给定字符串合并到数组中。 有一个字符串和两个数组:

const content = "0123456789TEXT";
const footnote_position  = [0,1,2,6]; // string positions 
const footnote_value = ["ZERO","ONE","TWO","SIX"]; // inserted values

但是对于我的代码以及上面给定的contentfootnote_positionfootnote_value,该算法必须输出如下:

["ZEROR","0","1","2345","SIX","67899TEXT"]

我的完整代码是:

const content = "0123456789TEXT";
const footnote_position = [0,6]; // must be sorted
const footnote_value = ["ZERO","SIX"];

const position_set = [...new Set(footnote_position)]; // must be sorted 1,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","","6789TEXT"]

出了什么问题?您是否有其他替代算法解决此问题?

另外,我真的不知道case length:为什么起作用。代码中没有定义的变量length

解决方法

使用forEachslice并维护last

更新:解决了最后一个元素的问题。很棒的建议@mplungjan,谢谢。

const text = "APPLE ORANGE";
const text_position = [0,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,"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);

,

我重写了代码以处理您的第二个示例

const mergeIt = (content,posArr,valArr) => {
  const arr = content.split("");
  posArr.sort((a,b) => a - b); // must be sorted
  while (posArr.length) {
    const pos = posArr.pop(); // destructive - you may want to clone
    const val = valArr.pop(); // destructive - you may want to clone
    if (val !== null) arr.splice(pos,`|${val}|`);
  }
  return arr.join("").split("|").filter(w => w)
};

let content = "0123456789TEXT";
let footnote_position = [2,1,2,6];
let footnote_value = ["ZERO","ONE","TWO","SIX"];
console.log(mergeIt(content,footnote_position,footnote_value))


const text = "APPLE ORANGE";
const text_position = [0,"zo"];
console.log(mergeIt(text,text_position,inserted_value))

// returns  ["yo","APPL","E O","RA","zo","NGE"]