1

I have an array from strings and numbers. I need to sort the numbers or better to extract only the numbers in another array. Here is the example:

 const myArr = ['Prihodi 23456 danaci 34 razhodi 23455 I drugi.']

I need to make it like this

 const filtered = [23456, 34, 23455]

I used split(' ') method to separate them with comma but don't know how to filter them for JS they all are strings.

2
  • Do you need two resulting arrays, one with strings another with numbers? is that? What code have you used to try until now? Commented Sep 5, 2018 at 16:21
  • The very fact that you are using the word "filter" should point you in the appropriate direction should you choose to research it further on your own Commented Sep 5, 2018 at 16:23

6 Answers 6

4

This could be a possible solution,

See MDN for map(), replace(), trim() and split()

const myArr = ['Prihodi 23456 danaci 34 razhodi 23455 I drugi.'];
filtered = myArr[0].replace(/\D+/g, ' ').trim().split(' ').map(e => parseInt(e));
console.log(filtered);

OR

const regex = /\d+/gm;
const str = `Prihodi 23456 danaci 34 razhodi 23455 I drugi`;
let m;
const filter = [];
while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }

  // The result can be accessed through the `m`-variable.
  m.forEach((match, groupIndex) => {
    filter.push(parseInt(match))
  });
}

console.log(filter);

Sign up to request clarification or add additional context in comments.

Comments

3

You can do it with simple Regex and Array.prototype.map:

const myArr = ['Prihodi 23456 danaci 34 razhodi 23455 I drugi.']

const result = myArr[0].match(/\d+/gi).map(Number);

console.log(result);

Comments

2

const myArr = ['Prihodi 23456 danaci 34 razhodi 23455 I drugi.'];
var result=[];
myArr.forEach(function(v){
  arr=v.match(/[-+]?[0-9]*\.?[0-9]+/g);
  result=result.concat(arr);
});
const filtered = result.map(function (x) { 
 return parseInt(x, 10); 
   });
console.log(filtered)

2 Comments

This returns an array of string representations of numbers, not what the OP requested.
changed it to numbers
1

const myArr = ['Prihodi 23456 danaci 34 razhodi 23455 I drugi.']
const reduced = myArr[0].split(' ').reduce((arr, item) => {
  const parsed = Number.parseInt(item)
  if(!Number.isNaN(parsed)) arr.push(parsed)
  return arr
}, [])
console.log(reduced)

Comments

0

I finish the task long time ago. However now I found this quick solution

const arr = ['Prihodi 23456 danaci 34 razhodi 23455 I drugi.']

const res = arr.join('')
.split(' ')
.filter(e => +e)
.map(num => +num);

console.log(res);

Comments

0

const array = ["string1", -35, "string2", 888, "blablabla", 987, NaN];

const mapArray = array.filter((item) => {
  if (item < 0 || item >= 0) return item;
});

console.log(mapArray);

1 Comment

Note that this would not work if the original array included negative numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.