0

Is there any way as to how I can convert the type of key's value from a string to a number(The string being a number) inside the object?

object={
   key1:"2",
   key2:1,
}

For example, I want to convert the key1's value type to a number, so that the value becomes a number instead of being in the string form. How can I do this?

  • Does this answer your question? [How to convert a string to an integer in JavaScript?](https://stackoverflow.com/questions/1133770/how-to-convert-a-string-to-an-integer-in-javascript) – BraveButter Nov 12 '20 at 06:33

6 Answers6

2

You can loop through the object with for in loop and and use Number()

let object={
  key1:"2",
  key2:1,
}    

for(let key in object){
  object[key] = Number(object[key])
}
Juha Kangas
  • 728
  • 5
  • 8
0

Call a conversion function and assign the result back to the property.

object.key1 = parseInt(object.key1);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can multiply them by 1.

let object={
   key1:"2",
   key2:1,
}

for(let key in object) {
   object[key] *= 1;
}

console.log(object);
bill.gates
  • 14,145
  • 3
  • 19
  • 47
0

You can conditional check for value[key]

 for(let k in object) {
       let keys = object[k]
        if((keys==Number(keys)){
           parseInt(object.key1)
        }
        else {
           process as it is..
        }
    }
DharmikSoni
  • 59
  • 1
  • 10
0

Just use '+' in front of string to convert it into number.

object={
   key1:"2",
   key2:1,
};
for(let key of Object.keys(object))  object[key] = +object[key]

console.log(object);
Durgesh
  • 205
  • 2
  • 9
0

To convert a string to a number if and only if it is a valid number you could try a conversion function like

const stringToNumber = str =>  typeof str == "string" && +str == str ? +str: str;
  • the type check makes sure boolean true and false aren't converted to numbers and also prevents converting falsey values to zero.
  • the + unary operator convverts a string operand to a number, or NaN if the string does not adhere to the syntax of a numeric literal,
  • the == tests if the explicit conversion performed by + produces the same result as an implicit conversion. If they same are same return the converted result, if not return the string argument.

Demonstration to convert numeric string properties of an object to numbers:

const stringToNumber = str =>  typeof str == "string" && +str == str ? +str : str;


const numberProperties = inputObj => {
   const outputObj = {};
   Object.keys( inputObj).forEach( key=> {
      outputObj[ key] = stringToNumber( inputObj[key]);
   });
   return outputObj;
}

console.log( numberProperties( {
   key1: "2",
   key2: 1,
   key0: "0",
   key02: 0,
   key3: false,
   key4: null,
   key9: "09"

}));

        

Update (in response to comment)

JavaScript Object keys are string values: if a number is provided as a property name (e.g. inside square brackets when using array lookup notation) the number is automatically converted to a string for use as the property name.

The short answer to the question, when interpreted literally, is "no, your can't convert the key values of an object, which are strings, to numbers inside the object's list of property names".

As an example, the keys of an Array object are strings:

const array = [1,2,3];
Object.keys(array).forEach(
  key=> console.log( "key: %s (%s), value: %s (%s)",
       key, typeof key, array[key], typeof array[key])
);
traktor
  • 17,588
  • 4
  • 32
  • 53