hm_patmol
hm_patmol

Reputation: 79

Javascript change Typeof

I am new in JavaScript.

My code allows me to list the elements of a JSON document as well as their types, and concatenate all its in a string donnees_types.

The problem is that the typeof in JavaScript returns only number for a number and string for a string. I want to return double instead of number and varchar instead of string in order to get:

temperature double, humidite double, built varchar, fire varchar

I know I can replace in my string using replace, but is there any other method to change the typeof?

 var event = {
  "temperature" : 12.45,
  "humidite" : 45.78,
  "built" : "charone",
  "fire" : "chartwo",
  "topic" : "sk1_001/data"
 };
 
var keys = Object.keys(event);

donnees_types = "";
 
for(var i= 0; i < keys.length -1; i++)
donnees_types +=  keys[i] + " " +  typeof(event[keys[i]]) + ", ";

var donnees_t = donnees_types.substring(0, donnees_types.length - 2);
console.log(donnees_t);
 

Upvotes: 0

Views: 4216

Answers (3)

talemyn
talemyn

Reputation: 7950

You might be able to get the functionality that you want by doing some additional a work to further classify the value based on what typeof returns and some custom logic, but you cannot change the functionality of typeof itself.

Pseudocode:

if `typeof` foo is `number`
    if `<insert integer rules here>`
        then foo is an integer
    else if `<insert float rules here>`
        then foo is a float
    else if . . .

The reason why you are running into this is that JavaScript is a "loosely typed" language . . . it does not recognize data types at a detailed level, but rather groups them into broader categories. These categories are the basis for the values that typeof returns.

You can read more about typeof here and JavaScript typing here, for a quick list of what you can expect, here are the values that typeof will return to you:

Type                                           Result

Undefined                                      "undefined"
Null                                           "object"
Boolean                                        "boolean"
Number                                         "number"
String                                         "string"
Symbol                                         "symbol"
Host object (provided by the JS environment)   Implementation-dependent
Function object                                "function"
Any other object                               "object" 

Upvotes: 0

neojg
neojg

Reputation: 221

Look below - but be aware of some artifacts of isNaN (see documentation)

 var event = {
  "temperature" : 12.45,
  "humidite" : 45.78,
  "built" : "charone",
  "fire" : "chartwo",
  "topic" : "sk1_001/data"
 };
 
var keys = Object.keys(event);

donnees_types = "";
 
for(var i= 0; i < keys.length -1; i++)
donnees_types +=  keys[i] + " " +  (isNaN(event[keys[i]])?'varchar':'double') + ", ";

var donnees_t = donnees_types.substring(0, donnees_types.length - 2);
console.log(donnees_t);

 

Upvotes: 0

Karl Reid
Karl Reid

Reputation: 2217

No, you can't change typeof. You could write your own function to map to the outputs you want.

function typeLabel(o){
  switch(typeof(o)){
    case "number" :
      return "double";
    case "string":
      return "varchar";
    default : 
      return typeof(o);
  }
}
var event = {
  "temperature" : 12.45,
  "humidite" : 45.78,
  "built" : "charonea",
  "fire" : "chartwo",
  "topic" : "sk1_001/data"
 };
 
var keys = Object.keys(event);

donnees_types = "";
 
for(var i= 0; i < keys.length -1; i++)
donnees_types +=  keys[i] + " " +  typeLabel(event[keys[i]]) + ", ";

var donnees_t = donnees_types.substring(0, donnees_types.length - 2);
console.log(donnees_t);

Upvotes: 1

Related Questions