【问题标题】:Generate merged array from a string by multiple positions of the string通过字符串的多个位置从字符串生成合并数组
【发布时间】:2020-12-01 22:44:59
【问题描述】:

例如,

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。

【问题讨论】:

  • 从给定的例子中我真的不明白这个算法背后的逻辑。你介意解释一下吗?
  • 是的,我添加了一个示例,请您重新阅读问题吗? @msmolcic

标签: javascript arrays algorithm merge


【解决方案1】:

我重写了代码来处理你的第二个例子

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, 0, `|${val}|`);
  }
  return arr.join("").split("|").filter(w => w)
};

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


const text = "APPLE ORANGE";
const text_position = [0, 4, 4, 7, 9];
const inserted_value = ["yo", "wo", "go", "lo", "zo"];
console.log(mergeIt(text, text_position, inserted_value))

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

【讨论】:

  • 差不多了,我在问题中添加了一个示例以进行更多说明。请您再看一遍帖子好吗?
  • 哇,非常感谢您的时间和帮助。现在可以了。
  • 它不如其他解决方案优雅,但其他解决方案需要将任何缺少的字符串添加到数组中
【解决方案2】:

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

【讨论】:

  • 差不多了,您的结果是[ "yo", "APPL", "wo", "go", "E O", "lo", "RA", "zo" ],预期结果是[ "yo", "APPL", "wo", "go", "E O", "lo", "RA", "zo", "NGE" ]
  • 如果 NGE 是固定的,那么这是更优雅的解决方案。我做过类似的工作,但已经有一个可行的例子
  • 我同意@mplungjan
  • 在结尾前加if (last &lt; text.length) output.push(text.slice(last))
  • 啊,我错过了@mplungjan,Nay Sie。非常感谢。 :)
猜你喜欢
  • 2011-12-02
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2023-03-09
  • 2021-04-07
相关资源
最近更新 更多