将字符串转换为二维数组

时间:2019-07-23 23:24:32

标签: javascript multidimensional-array

例如,每当出现x时,我都会得到一个字符串(示例=“。| x”),每当'。'时,我都必须将true推入数组。弹出窗口,我必须将false推送到我的数组。而我的问题所在是,每当“ |”弹出窗口,我必须开始一个新的布尔值行,使其成为2d数组

function parse(str) {
    let result = [];
    let array = [];
    for (let char of str){
        if (char === "x") {
            result.push(true);
        } if (char === "."){
            result.push(false);
        } if (char === "|"){
            result.push(array);

        }
    }
    return [result];
}

this code just returns [[false, [], true]]

这就是我应该得到的

[[false],[true]]

5 个答案:

答案 0 :(得分:0)

function parse(str) {
    let result = [];
    let array = [];
    for (let char of str){
        if (char === "x") {
            array.push(true);
        } if (char === "."){
            array.push(false);
        } if (char === "|"){
            result.push(array);
            array = []
        }
    }
    return result;
}

这将创建一个新行,以便您添加更多的bool值(如果我从您的问题中了解到这一点)。

答案 1 :(得分:0)

尝试这个:

function parse(str) {
    let result = [];
    for (let char of str){
        if (char === "x") {
            result.push(true);
        } if (char === "."){
            result.push(false);
        } if (char === "|"){
            result = result.map( e => [e]);
        }
    }
    return result;
}

console.log(parse('.|x')); // logs [ [ false ], true ]

答案 2 :(得分:0)

迭代解决方案。

function parse(str) {
  const result = [];
  let array = [];
  for (const char of str) {
    switch (char) {
      case 'x':
      case '.':
        array.push(char === 'x');
        break;
      case '|':
        result.push(array);
        array = [];
    }
  }
  return result;
}

功能解决方案。

const parse = (str) => str.split('').reduce((acc, char) => {
  switch (char) {
    case 'x':
    case '.':
      acc[acc.length-1].push(char === 'x');
      break;
    case '|':
      acc.push([]);
  }
  return acc;
}, []).slice(0, -1);

两个解决方案都在O(n)时间内运行。我是根据这样的假设编写的:如果字符串包含另一个字符(该函数可能抛出Error?),该函数什么都不做,并且只有在至少有{{时,才应将任何子数组推送到2D列表中。 1}}个礼物。如果字符串不包含'|',则它将返回一个空数组。

答案 3 :(得分:0)

  1. 在每个管道上分割字符串:|

    string.split('|')

  2. 遍历结果数组,其中.map()方法的回调采用每个字符串并将其转换为字符数组。

    string.split('|') .map(str => [... [... str] step 3 ])

    外括号和点扩展运算符: [... [...str] step 3 ] 会将step 3的返回值转换为数组。内括号和传播运算符:[... [... str] step 3 ]会将字符串拆分为字符。

  3. 然后使用.flatMap()方法(来自step 2.map()方法的组合,通过https://lkcfesnotification.000webhostapp.com/admin迭代来自.flat()的每个字符数组。在回调中,返回值应作为单独的子数组返回-此外,如果您需要省略返回值,只需传递一个空数组即可。在此回调中,它将把每个字符与'x'进行比较,并返回[true][false]注意:返回的数组被展平)。

    string.split('|').map(str => [...[...str] .flatMap(chr => chr ==='x'?[true]:[false]) ])

// Raw data for a 6x8 table
const data = `........|....xxxx|xxxxxxxx|x.x.x.x.|x..xx..x|.xx..xx.`;

const bool2D = string => string.split('|').map(str => [...[...str].flatMap(chr => chr === 'x' ? [true] : [false])]);

console.log(JSON.stringify(bool2D(data)));

答案 4 :(得分:0)

此代码可以解决问题

function parse(str) {
    let result = [];
    let array = [];
    for (let char of str){
        if (char === "x") {
            array.push(true);
        } if (char === "."){
            array.push(false);
        } if (char === "|"){
            result.push(array);
            array = []
        }


    }
    result.push(array);
    return result;
}