0

i like to convert a type given by a string, because i want to convert a string of numbers to an array of numbers.

var myStringType = "number[]";
var myString = "0, 0, 0";

var myStringArray = myString.Split(","); <- these array need to be a array;
var myNumbersArray = ConvertToAnyByString(myStringArray, myStringType);

I can convert by knowing the type, if the method above is not possible. Is there something like compare against typeof(number[])

Update: I think here is a misunderstanding. I don't know which type will passed to my function. I need type check that or convert it by a string information which type it is. And there is no way to do that natively in javascript, because there is no type of arrayOfStrings there is just a array. But i can make a helper functions to do this by hand.

3
  • Not sure what you're trying to ask but if you do '0,0,0'.split(',') that'll actually give you an array ["0", "0", "0"]. Commented Apr 13, 2013 at 3:17
  • @Rishabh I've a string and it need to be a number array not a string array. Commented Apr 13, 2013 at 3:18
  • You need to clarify 2 things please: * Do you expect to always have numbers in the array, or do you need to filter out errors (empty slots, text)? * Ints or Floats? Commented Apr 13, 2013 at 3:22

2 Answers 2

2

OK. try this -

var str = '0,0,0';
var arr = str.split(','); // ['0', '0', '0']
var int_arr = arr.map(function(el) {return parseInt(el, 10);}); // [0, 0, 0]

Hope that helps!

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

3 Comments

Sure the OP expects only Ints, & not Floats?
note that prototype map on array is new ES5 and thus not browser-compatible at all. Also, the shim needed is quite big.
@GitaarLAB we can't all dwell in the past :) besides, the shim can be made quite small.
0

Based on your comment you can do (without .map or shim for .map):

var str = '0,1,8';
var arr = str.split(','), L=arr.length;
          while(L--){arr[L]=Number(arr[L]);}
//now arr contains numbers.

here Number(arr[L]); is the 'payload' but you also could use (depending on what you need):
parseInt(arr[L]); or parseFloat(arr[L]); or arr[L]*1; or even simply +arr[L];
as a payload. It's just what floats your boat.

test with: alert(typeof arr[1]); // number


But to actually answer the question that you originally asked (convert values in a array to a different type specified by a string), I offer this simple example to give an idea how one could do this:

var convertArrToAnyByString = function(arr,converTo,rad){
   var L = arr.length;
   rad = rad||10;  // optional, defaults to 10 as radix
   switch(converTo){
     case 'number':
        while(L--){arr[L] = Number(arr[L]);}
        break;
     case 'string':
        while(L--){arr[L] = arr[L].toString(rad);}
        break;
     default:
        arr=false;
   }
   return arr;
};

var str = '0,1,8';
var arr = str.split(','); 

// testing:
alert(typeof arr[1]);  //arr[1] should be string by default
arr=convertArrToAnyByString(arr,'number');
alert(typeof arr[1]);  // number
arr=convertArrToAnyByString(arr,'string');
alert(typeof arr[1]);  // string again
alert(arr[2]);         // still 8

You can easily expand on this example by adding types and even error-checking.

Good Luck!

1 Comment

@Jack: thank you for the kind nudge. I've converted from lower case l to uppercase L just last month and missed that last l. Updated answer. Yes, tested.

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.