1

I'm quite new to programming, and I feel that similar questions have been asked before. But I've tried to apply them and know I am missing something fundamental.

Given an array:

var myArray = [24.203, 12*45, 000-1, 4567+00];

I'd like to strip all non integers, so that I have something like this:

var myArray = [24203, 1245, 0001, 456700];

I know of the .replace method, but I can't seem to get it work. Here are four things I've tried:

function stripNonIntegers(arr) {
    var x;
    this.myArray = myArray;
for(x = 0; x < 10; x++) {
    myArray[x] = myArray[x].replace(/\D/g, '');
} }
stripNonIntegers(myArray);

This returns an error saying myArray is undefined.

var x;(myArray); { //I don't like the semicolons but I get an error if I omit them
for(x = 0; x < 10; x++)
    {myArray[x] = myArray[x].replace(/\D/g, '');
    } }

this returns an error saying x is undefined.

    stripNonIntegers= function(arr) {
        for (x =0; x<this.length; x++)
    myArray.replace(/\D/g,'');};
stripNonIntegers(myArray);

This output is undefined.

var stripNonIntegers= myArray[x]; {
    for (x=0; x<myArray.length; x++) {
stripNonIntegers = myArray.replace(/[^\d]/g, '');
} }

And this one also says x is undefined. This post explains how to use the .replace method with a regex of /D to strip non-numerics from a string, but I can't seem to get it to work with an array (not a function). Thus I try to stick a 'for' loop in there so it treats each element as its own string. I know I'm making a stupid mistake, but for all my trying, I can't identify it. I'm shooting in the dark.

Any tips? Thanks in advance.

Community
  • 1
  • 1
Allen F
  • 13
  • 4
  • 1
    Your array consists of these numbers (not strings) after initialization: [ 24.203, 540, -1, 4567 ]. Furthermore, you're referencing myArray, but the signature head expects "arr". – manonthemat May 09 '16 at 20:24
  • 1
    One of the issues here is that the values in your array don't actually appear to be strings but instead the results of arithematical operations, which will likely result in their results being stored in the array as opposed to the expressions themselves `[24.203,540, 4567]`. Do you have any other code related to how you are building these expressions? If they are "hard-coded" like this, you may not have much flexibility with what you can do. – Rion Williams May 09 '16 at 20:26
  • It's a personal project I'm working on for practice, and the rest of my program works with both strings and integers as array elements. I had them in quotation marks (turning them into strings) but that didn't seem to affect any of the outcomes. Though I admit I've tried so many things that it's hard to keep track exactly... – Allen F May 09 '16 at 20:37

2 Answers2

3
var myArray = ['24.203', '12*45', '000-1', '4567+00'];

var myNewArray = myArray.map(function(value) {
    return parseInt(value.replace(/\D/g, ''), 10);
});

\D is a shorthand character class that matches all non-digits.

The above code works when your array elements are strings.

michaelgmcd
  • 2,369
  • 1
  • 18
  • 24
  • You should probably advise declaring the array with strings `var myArray = ['24.203', '12*45', '000-1', '4567+00'];` – IrkenInvader May 09 '16 at 20:32
  • I agree. Updated the answer. @andrew-burgess's is a little more verbose but the same thing. – michaelgmcd May 09 '16 at 20:35
  • This worked, thanks! BTW, what purpose does the "10" after the parseInt serve? It seems to work without it. – Allen F May 09 '16 at 20:47
  • Don't forget to accept the answer :) As for the "10", check out the **parseInt** docs! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt. In short, it is the **radix** parameter (the base in mathematical numeral systems). It is always best to include this as different browsers have different implementations. Usually, the default is 10, which corresponds to our normal decimal system. – michaelgmcd May 09 '16 at 20:50
2

You have the regular expression right, and you're using replace correctly. Your problem may be that the items in your array are not strings in the code you gave. Just wrap each one in quotes to make them strings:

var myArray = ['24.203', '12*45', '000-1', '4567+00'];

Then, you can map over the array and use your replace call:

var newArray = myArray.map(function (item) {
    return item.replace(/\D/g, '');
});

That results in this:

["24203", "1245", "0001", "456700"]

These are still strings, though. To convert them to numbers, you can use parseInt:

var intArray = newArray.map(function (item) {
    return parseInt(item, 10);
});

You could do all this in one step, if you prefer:

var newIntArray = myArray.map(function (item) {
    return parseInt(x.replace(/\D/g, ''), 10);
});

Of course, if you prefer regular for loops, you could do all this with those. However, I think using map looks a bit cleaner. If you're not familiar with map, check out the MDN docs for the array map function

Andrew Burgess
  • 1,765
  • 17
  • 19
  • I tried this, and it still says x is undefined. And putting a 'for' statement in there doesn't solve the problem. +1 for the integers into strings tip, though! – Allen F May 09 '16 at 20:44