2

I'm trying to convert object values into integers:

var array = { 
  borderTopLeftRadius:"0px", 
  borderTopRightRadius:"0px", 
  borderBottomLeftRadius:"0px",
  borderBottomRightRadius:"0px"
}

I want to keep the same structure, but the result should look like this:

var array = { 
  borderTopLeftRadius:0, 
  borderTopRightRadius:0, 
  borderBottomLeftRadius:0,
  borderBottomRightRadius:0
}

I've been using $map to do it, but I cant make it work

var newArray = $.map(array, function(value){
    return parseInt(value, 10);
});
5
  • 5
    Sidenote: Your array isn't actually an array. It's an object. Commented Dec 15, 2016 at 17:26
  • Yes, it say in the title and description as well. Commented Dec 15, 2016 at 17:27
  • @Alko But you're trying to map an object...? Commented Dec 15, 2016 at 17:29
  • @AndrewLi: parseInt("0px", 10) works as expected. Commented Dec 15, 2016 at 17:29
  • @RocketHazmat Yes, forgot that if the number is the first thing in the string, non-numericals are stripped. Commented Dec 15, 2016 at 17:30

3 Answers 3

3

First of all you've an Object literal {} and not an array [], you could use jQuery method .each() to achieve that :

var myObject = { 
  borderTopLeftRadius:"0px", 
  borderTopRightRadius:"0px", 
  borderBottomLeftRadius:"0px",
  borderBottomRightRadius:"0px"
};
var newObject = {};

$.each( myObject, function( key, value ) {
  newObject[key]=parseInt(value, 10);
});

console.log(newObject);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

2 Comments

Thanks it works perfect. I have accepted your answer. I never said the I want to convert an array. Title says an Object. Just because I named variable array, it dos not mean that. It was for demo purpose.
Just to be clear alko people are confused because you used the arrays tag which suggests the post is about arrays. And a variable name is supposed to be suggestive as to what the variable holds.
2

Your array is not an array. JavaScript does not have associative arrays, it has objects - which is what you have.

jQuery's $.map function works on both arrays and objects, but it only returns arrays. Thus, your object is converted into an array.

You could loop over the keys of the object and build a new object with the integer values:

var newArray = {};
Object.keys(array).forEach(function(key){
    newArray[key] = parseInt(array[key], 10);
});

Comments

0

You can achieve the result with a for loop

var array = { 
  borderTopLeftRadius:"0px", 
  borderTopRightRadius:"0px", 
  borderBottomLeftRadius:"0px",
  borderBottomRightRadius:"0px"
}

for (var key in array) {
  array[key] = parseInt(array[key],10);
}

console.log(array);

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.