简体   繁体   English

如何删除嵌套数组中的特殊字符?

[英]How can I delete special characters inside a nested array?

So let's say I have an array like this, what would be the most effiecient way to go through the array and erase all the '$' signs? 所以,假设我有一个像这样的数组,那么最有效的方法是通过数组并擦除所有'$'符号?

I have tried many different approaches but none of them seem to work properly, any thoughts? 我尝试了很多不同的方法但是它们似乎都没有正常工作,任何想法?

const myArray = [
        ['$','H','e','$','$','l'],
        ['l','$','o','$','W','o'],
        ['r','l','$','d','$','M'],
        ['y','$','N','a','$','m'],
        ['e','$','i','s','$','p'],
        ['a','b','$','l','$','$'],
        ['$','o','$','$','w','$']
    ];

A nested for loop will work very quickly: 嵌套的for循环将非常快速地工作:

for (var i = 0; i < myArray.length; i++){
    for (var j = 0; j < myArray[i].length; j++){
      if (myArray[i][j]=='$')
          myArray[i][j]==''
    }
}

You can just filter the individual Arrays in myArray like so: 您可以像这样过滤myArray中的各个数组:

for (let i = 0; i < myArray.length; i++) {
    myArray[i] = myArray[i].filter(x => x != '$');
}
const myArray = [
    ['$','H','e','$','$','l'],
    ['l','$','o','$','W','o'],
    ['r','l','$','d','$','M'],
    ['y','$','N','a','$','m'],
    ['e','$','i','s','$','p'],
    ['a','b','$','l','$','$'],
    ['$','o','$','$','w','$']
];

const result = myArray.map(arr => arr.filter(letter => letter != '$'));
console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM