0

How do I extract a value from a database item in an array that contains both text and the value I want.

For example the first item in array:

resultArray[i].selector_sliderdata_attributes

is the value: data-time-build='100'

The second item has the value: data-time-build='450' and so on.

I want to be able to extract the 100 and 450 in my js function and use it to compare values in my isotope slider.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Frank
  • 89
  • 2
  • 12

2 Answers2

1

Hello you can use regex:

var NumOnly = YourString.replace(/[^0-9]/g, '');

Hope it helps.

AppBroom
  • 34
  • 4
  • I tried this one first because it was so simple to insert into my code and it worked perfectly the first time. Awesome. So my solution was: var NumOnly = resultArray[i].selector_sliderdata_attributes.replace(/[^0-9]/g, ''); Thank you. – Frank May 13 '15 at 15:03
1

You want to map your array to a number map like so:

array.map(function(value, index){
    return +value.match(/\d+/);
});

* the + turns the result into a number

Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98
  • I wasn't really sure how to use this solution so I tried the one below first. It worked, but I would be curious as to how this would be implemented. – Frank May 13 '15 at 15:06
  • For an array of strings it will return an array of numbers within the strings – Nachshon Schwartz May 13 '15 at 17:37