简体   繁体   English

尝试删除/移除数组中的特定字符串时如何使用 .splice()?

[英]How can I use .splice() when trying to delete/remove specific strings in an array?

I've created a successful way of removing the strings in an array, but is there a way to remove specific parts in an array using .splice()我已经创建了一种删除数组中字符串的成功方法,但是有没有一种方法可以使用 .splice() 删除数组中的特定部分

const arr = [1000, 2000, "numbers", 5000, 6000, "integers"];

getInt = (arr) => {
  return arr.map((i)=> {if (/^\d+$/.test(i))
   return i;
  });
}
getInt(arr);

console.log(getInt(arr));
const arr = [1000, 2000, "numbers", 5000, 6000, "integers"];

console.log(arr.filter(item =>  Number.isInteger(item)))

You can use filter method to filter out the integers, give it a try mate.您可以使用 filter 方法过滤掉整数,试试看吧。

let arr = [1000, 2000, "number", 5000, 6000, "integers"];
getInt = (arr) => {
  for (i=0; i<arr.length; i++) {
    if (Object.prototype.toString.call(arr[i]) === "[object String]") {
      arr.splice(i, 1)
    }
  };
}
getInt(arr)
console.log(arr) // [1000, 2000, 5000, 6000]

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

相关问题 Javascript:如何在数组上使用splice()移动特定单词? - Javascript: How can I use splice() on an array to move a specific word? 如何使用for循环和拼接来删除单词,然后检查数组中的特定单词 - How to use a for loop and splice to remove a word and then check an array for a specific word 使用Splice()函数,如何删除特定的数组元素? - Using Splice() function, how do I remove specific array elements? 拼接或删除数组中的特定元素 - Splice or remove specific element in an Array 如何使用数组splice()函数删除对象中的子数组? - How do I use the array splice() function to remove a sub array in an Object? 如何通过拼接删除数组值? - How to delete array value by splice? 使用一个字符串数组来拼接另一个数组? - Use one array of strings to splice another array? 我该如何编写Google表格脚本来对特定列进行排序,并删除所有不在特定字符串数组中的内容 - How can I write a google sheets script that will sort through a particular column and delete everything that is not in a specific array of strings 如何使用 express.js app.delete 从数组中删除特定的 object? - How do I use express.js app.delete to remove a specific object from an array? 如何使用splice删除数组中每个字符串的第一个字母? - How do I remove the first letter of every string in array with splice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM