2

I want to convert a string into an array. that works only with number value. in the following example the "border_color & border_style keys" returning NaN as value.

var str ="margin_top=5&margin_bottom=5&border_color=#dfdfdf&border_style=solid";
strToArray(str);

function strToArray(str){
    str = str.replace(/\(|\)/g,'');
    var arr = str.split('&');
    var obj = {};
    for (var i = 0; i < arr.length; i++) {
        var singleArr = arr[i].trim().split('=');
        var name = singleArr[0];
        var value = singleArr[1]-0;
        if (obj[name] === undefined) {
            obj[name] = value;
        }
        alert(name+': '+value);
    }
    return obj;
}

3
  • 1
    var value = singleArr[1] - 0;??? What do you think the results of "#dfdfdf" - 0 and "solid" - 0 are? Commented May 10, 2018 at 23:48
  • what should the outcome be? array of arrays? Commented May 10, 2018 at 23:49
  • my bad. I didn't see it. thanks Commented May 10, 2018 at 23:59

2 Answers 2

4

The NaNs are comming from trying to convert the non-numeric values into numbers (ie, "#dfdfdf" and "solid"). Before trying to convert to numbers, check if the value string is valid or not using isNaN:

var value = singleArr[1];                         // don't convert yet
if (obj[name] === undefined) {
    obj[name] = isNaN(value)? value: +value;      // if value is not a valid number, then keep it as it is (a string). Otherwise, convert it to a number (using unary + which is shorter than substracting 0)
}

Working example:

var str ="margin_top=5&margin_bottom=5&border_color=#dfdfdf&border_style=solid";
strToArray(str);

function strToArray(str){
    str = str.replace(/\(|\)/g,'');
    var arr = str.split('&');
    var obj = {};
    for (var i = 0; i < arr.length; i++) {
        var singleArr = arr[i].trim().split('=');
        var name = singleArr[0];
        var value = singleArr[1];
        if (obj[name] === undefined) {
            obj[name] = isNaN(value)? value: +value;
        }
        alert(name+': '+value);
    }
    return obj;
}

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

Comments

0

Not really sure the way you want to return, but you can use: Object.values()

var str ="margin_top=5&margin_bottom=5&border_color=#dfdfdf&border_style=solid";
strToArray(str);

function strToArray(str){
	str = str.replace(/\(|\)/g,'');
	var arr = str.split('&');
    var keys = []
    var values = []
	var obj = {};
	for (var i = 0; i < arr.length; i++) {
		var singleArr = arr[i].trim().split('=');           
        keys.push(Object.values(singleArr)[0])
        values.push(Object.values(singleArr)[1])          
       
	}

   alert(values)
   alert(keys)

	return obj;
}

Comments

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.