I would like to know how to convert object properties string to integer in javascript.
I have a obj, which if has property value is number string convert to number in javascript
var obj={
ob1: {id: "21", width:"100",height:"100", name: "image1"},
ob2: {id: "22", width:"300",height:"200", name: "image2"}
}
function convertIntObj (obj){
Object.keys(obj).map(function(k) {
if(parseInt(obj[k])===NaN){
return obj[k]
}
else{
return parseInt(obj[k]);
}
});
}
var result = convertIntObj(obj);
console.log(result)
Expected Output:
[
{id: 21, width:100,height:100, name: "image1"},
{id: 22, width:300,height:200, name: "image2"}
]
returninconvertIntObj, so the value of the function call is implicitlyundefinedNaNis not equal to anything, not even to itself. Also, you need to map the properties ofobjindividually, currently you're parsing the objects inobj, not their properties.