最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove Strings, Keep Numbers In Array With JavaScript - Stack Overflow

programmeradmin0浏览0评论

Other articles talk about removing strings from an array based on a search term.

But I'm trying to indentify which elements are strings and which elements are numbers in an array, and then remove all strings to return a new array.

function filter_list(l) {

  let newArray = []; 

  for (let i = 0; i < l.length; i ++) {

     if (i !== "^[a-zA-Z0-9_.-]*$") {
       newArray = newArray + i;
     }
  }

  return newArray;

}

This is returning 0123.

  1. Why is it not returning an array?
  2. Why is if (i !== "^[a-zA-Z0-9_.-]*$") not working? How else can I check for when an element is a string (something in quotes) within the array?

Thanks

Other articles talk about removing strings from an array based on a search term.

But I'm trying to indentify which elements are strings and which elements are numbers in an array, and then remove all strings to return a new array.

function filter_list(l) {

  let newArray = []; 

  for (let i = 0; i < l.length; i ++) {

     if (i !== "^[a-zA-Z0-9_.-]*$") {
       newArray = newArray + i;
     }
  }

  return newArray;

}

This is returning 0123.

  1. Why is it not returning an array?
  2. Why is if (i !== "^[a-zA-Z0-9_.-]*$") not working? How else can I check for when an element is a string (something in quotes) within the array?

https://www.codewars./kata/list-filtering/train/javascript

Thanks

Share Improve this question edited Sep 11, 2021 at 17:57 HappyHands31 asked Mar 28, 2019 at 17:00 HappyHands31HappyHands31 4,10119 gold badges65 silver badges117 bronze badges 4
  • 1 newArray = newArray + i; is not how adding new elements to an existing array works in JavaScript – Andreas Commented Mar 28, 2019 at 17:02
  • 1 You should use push – Code Maniac Commented Mar 28, 2019 at 17:02
  • What is your input? – Maheer Ali Commented Mar 28, 2019 at 17:03
  • @MaheerAli in this case it's ([1,2,'a','b']) – HappyHands31 Commented Mar 28, 2019 at 17:04
Add a ment  | 

5 Answers 5

Reset to default 4

You can is typeof keyword. and filter(). I have tested the code its passing all tests in codewars.

Using ES6 Arrow Function

function filter_list(l) {
  return l.filter(x => typeof x === "number");
}
console.log(filter_list([1,2,'a','b']))

Without Arrow Function

function filter_list(l) {
  return l.filter(function(x){
      return typeof x === "number"
    });
}
console.log(filter_list([1,2,'a','b']))

Using Simple Loops

function filter_list(l) {
  let newArr = [];
  for(let i = 0;i<l.length;i++){
    if(typeof l[i] === "number") newArr.push(l[i]);
  }
  return newArr
}
console.log(filter_list([1,2,'a','b']))

Regex is not good way to parse such table. Try isNaN

console.log(
    [1,2,3,4,5, 'a', 'b', 1, 3].filter(item => !isNaN(item) ? item : '')
)

If you want less hacky way try

function filter_list(l) { 
// l is very bad name, because look similar to i

  let newArray = []; 

  for (let i = 0; i < l.length; i ++) {
      !isNaN(l[i]) ? newArray.push(l[i]) : ''
  }
  return newArray;
}

or even

  for (let i = 0; i < l.length; i ++) {
      !isNaN(l[i]) ? newArray[i] = l[i] : ''
  }

Hovewer, this task can be done with regexes, but I cannot remend this solution.


[1,2,3,4,5, 'a', 'b', 1, 3].join(' ').replace(/\D/gm, '').split('')

var numberArray: any[];
numberArray.filter(Number)

Using this you can filter only numbers in an array and then can performe what you want.

I worked out a simple answer that will work as well using the same logic required to solve your problem. I used it on an example where you have an array of temperature values, and you want to remove all the values which are strings from the existing array, then populate the new empty array.You can use typeof operator to identify the type of value in the temperatures array at position i which is the index of that array element. If the type of that value is not a string then push the value of the temperatures array at the current index position to the new array.

const temperatures = [3, -2, -6, -1, 'error', 9, 13, 17, 15, 14, 9, 5];

const cleanTemperatures = [];

for (let i = 0; i < temperatures.length; i++) {
  if (typeof temperatures[i] !== 'string') {
    cleanTemperatures.push(temperatures[i]);
  }
}

function filter_list(l) {
  return l.filter(x => typeof x === "number");
}
console.log(filter_list([1,2,'a','b']))

发布评论

评论列表(0)

  1. 暂无评论