替换嵌套数组中的所有元素

时间:2018-06-02 22:02:27

标签: javascript

我有一个挑战,我一直在争夺一段时间。它是关于替换网格形式的数组中的所有元素,但我的解决方案只是替换所选择的元素而不是我想要的。

在这个挑战中,我想用字符串“even”替换可被2整除的整数值,而其余的值替换为字符串“odd”。

/ *  * - numbers变量是一个数组数组。  * - 嵌套for循环以循环numbers。  * - 它将每个偶数转换为字符串“even”  * - 并将每个奇数转换为字符串“odd”  * /

var myNumbers = [
    [243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
    [34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
    [67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
    [12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
    [4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
    [5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
    [74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
    [53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
    [67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
    [76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];

for(var row=0; row<myNumbers.length; row++) {
    for(var column=0;column<myNumbers[row].length;column++) {
        if(myNumbers[column]%2===0){
            myNumbers[column].splice(column,1,"even");
        }else{ 
            myNumbers[column].splice(column,1,"odd");
        }
        console.log(myNumbers[row][column]);
     }
} 

代码输出: 奇 12 23 12 45 45 78 66 223 3 34 奇 1 553 23 4 66 23 4 55 67 56 奇 553 44 55 五 428 452 3 12 31 55 奇 79 44 674 224 4 21 4 2 3 52 奇 51 44 1 67 五 五 65 4 五 五 奇 五 43 23 4424 74 532 6 7 35 17 奇 43 43 66 53 6 89 10 23 52 111 奇 109 80 67 6 53 537 2 168 16 2 奇 8 76 7 9 6 3 73 77 100 56 奇

4 个答案:

答案 0 :(得分:2)

可能更容易使用嵌套的map

var myNumbers = [
    [243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
    [34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
    [67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
    [12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
    [4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
    [5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
    [74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
    [53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
    [67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
    [76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = myNumbers.map(row => row.map(num =>
  num % 2 === 0
  ? 'even'
  : 'odd'
));
console.log(output);

使用for循环实现同样的事情很多更加冗长和混乱,在大多数情况下不应该这样做(数组方法有更好的抽象,不需要手动迭代),但如有必要:

var myNumbers = [
    [243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
    [34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
    [67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
    [12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
    [4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
    [5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
    [74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
    [53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
    [67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
    [76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = [];
for (let rowIndex = 0; rowIndex < myNumbers.length; rowIndex++) {
  const row = myNumbers[rowIndex];
  const newRow = [];
  for (let colIndex = 0; colIndex < row.length; colIndex++) {
    const num = row[colIndex];
    newRow.push(num % 2 === 0 ? 'even' : 'odd');
  }
  output.push(newRow);
}

console.log(output);

答案 1 :(得分:1)

您对行索引使用了错误的变量

myNumbers[column]
//needs to be
myNumbers[row]

你的if条件是使用错误的行索引,并试图与整个数组进行比较而不是数组中的值

if(myNumbers[column]%2===0)
//needs to be
if(myNumbers[row][column]%2===0)

演示

&#13;
&#13;
var myNumbers = [
  [243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
  [34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
  [67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
  [12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
  [4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
  [5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
  [74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
  [53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
  [67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
  [76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];

for (var row = 0; row < myNumbers.length; row++) {
  for (var column = 0; column < myNumbers[row].length; column++) {
    if (myNumbers[row][column] % 2 === 0) {
      myNumbers[row].splice(column, 1, "even");
    } else {
      myNumbers[row].splice(column, 1, "odd");
    }
  }
}
console.log(myNumbers);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

在内部循环中,您需要使用两个索引(即myNumbers[row][column])访问值。就目前而言,您只使用列索引,因此将值拼接到包含行的数组中。

答案 3 :(得分:0)

为什么不简单地设置myNumbers[row][column]的值而不是使用splice

var myNumbers = [
    [243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
    [34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
    [67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
    [12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
    [4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
    [5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
    [74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
    [53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
    [67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
    [76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];

    for(var row=0; row<myNumbers.length; row++) {
         for(var column=0;column<myNumbers[row].length;column++) {
                if(myNumbers[row][column]%2===0) {
                  myNumbers[row][column] = "even";
                } else{ 
                    myNumbers[row][column] = "odd";
                }
            console.log(myNumbers[row][column]);
     }
    }