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)
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"}
]
Share
Improve this question
edited Apr 6, 2020 at 10:19
VLAZ
29.1k9 gold badges62 silver badges84 bronze badges
asked Apr 6, 2020 at 10:17
SenSen
7733 gold badges14 silver badges35 bronze badges
2
-
1
You don't have any
return
inconvertIntObj
, so the value of the function call is implicitlyundefined
– VLAZ Commented Apr 6, 2020 at 10:20 -
2
... and
NaN
is not equal to anything, not even to itself. Also, you need to map the properties ofobj
individually, currently you're parsing the objects inobj
, not their properties. – Teemu Commented Apr 6, 2020 at 10:21
4 Answers
Reset to default 10This should do the work:
var obj = {
ob1: {
id: "21",
width: "100",
height: "100",
name: "image1"
},
ob2: {
id: "22",
width: "300",
height: "200",
name: "image2"
}
}
function convertIntObj(obj) {
const res = {}
for (const key in obj) {
res[key] = {};
for (const prop in obj[key]) {
const parsed = parseInt(obj[key][prop], 10);
res[key][prop] = isNaN(parsed) ? obj[key][prop] : parsed;
}
}
return res;
}
var result = convertIntObj(obj);
console.log('Object result', result)
var arrayResult = Object.values(result);
console.log('Array result', arrayResult)
Click "Run code snippet" so see the result
Iterating over Object.keys() twice. If the value corresponding to the key is a number then parseInt the value else set the default value which was present earlier
var obj = {
ob1: { id: "21", width: "100", height: "100", name: "image1" },
ob2: { id: "22", width: "300", height: "200", name: "image2" }
};
var res = {};
Object.keys(obj).forEach(key => {
res[key] = {};
Object.keys(obj[key]).forEach(temp => {
res[key][temp] = !isNaN(obj[key][temp])
? parseInt(obj[key][temp], 10)
: obj[key][temp];
});
return res;
});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use Object.entries()
and .reduce()
methods to iterate over the key value pairs in your data and use Number
and Number.isNaN()
methods to transform the values appropriately.
const data = {
ob1: {id: "21", width:"100",height:"100", name: "image1"},
ob2: {id: "22", width:"300",height:"200", name: "image2"}
};
const result = Object.entries(data).reduce((r, [k, o]) => {
r[k] = Object.entries(o).reduce((r, [k, v]) => {
let _v = Number(v);
if(!Number.isNaN(_v)) { v = _v; }
return (r[k] = v, r);
}, {});
return r;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hi I would remend you to use the JSON.stringify() method. It is used to convert object to string which is needed to send data over the web server. It converts the set of variables in the object to a JSON string:
var objToStr = {
siteName: "W3Docs",
bookName: "Javascript",
booksCount: 5
};
var myJSON = JSON.stringify(objToStr);
console.log(myJSON);
Also, you can use the The toString() method. It is also called when you need to convert the object into a string:
var obj = {
siteName: "W3Docs",
bookName: "Javascript",
booksCount: 5
};
function objToString(object) {
var str = '';
for (var k in object) {
if (object.hasOwnProperty(k)) {
str += k + '::' + object[k] + '\n';
This information is taken from this source.