Why is object property automatically transformed into a string?

Asked

Viewed 118 times

1

I’m using Nodejs and I have this problem:

I have the following object:

let obj = { id : { $gte : 5 } };

console.log(obj);
//Resultado: { id: { '$gte': '5' } }

Note that in the result, the property $gte is in quotes. Like a string.

How to escape from it ? I don’t want the property $gte is in quotes.

Apparently this only happens inside the Node. I tested the code in the browser console and works as expected.

  • 1

    Good afternoon, but that’s just the visual, if you give a typeof on the object and on all Keys you see the types correctly. That will be in order console.log(typeof obj) object, console.log(typeof obj.id) object and console.log(typeof obj.id.$gte) number.

  • Well observed. I hadn’t thought so. The problem is that if I play the variable obj in a function, this function recognizes the property $gte as string. But it must be for some mistake, not the one I asked the question. Thanks for the help.

  • 1

    If you can share your method, we can see what happens. You can use parseint or parseFloat to leave as type number.

  • 2

    Keys are converted to strings, see examples here and the documentation here

  • 3

    $gte being converted to string is kind of obvious, what’s weird there is the value 5 have turned string. I just ran this code on Node V10.15.0 and it’s correct. O 5 remains as Number.

2 answers

6

Appears as string because the key is string, has always been, so there’s not much to escape from. You can escape being printed like this and then it is very simple not to print something that was done for debugging and not to show to the end user.

I always say:

Fiat 147 detonado andando pelas ruas

People see a result and assume that they can use it the way they want, but that’s not how you program it. To program correctly you need to understand all the computational concepts and master the documentation of the tool you are using. Once this is done you will know that what is printed when an object is printed is a value that helps the programmer, and it cannot be considered a valid value to be used for anything. Some very simple object values can be used this way, but left the obvious can no longer. If you want to print all the elements of the object you must provide an algorithm that does this, and then you can configure it the way you want it to be printed.

There are several ways to do this and here at Sopt is full of examples, I remember have answered a. Note that it is not in quotes, which is just a detail. Others show other forms.

5


When you do console.log({ id : { $gte : 5 } }); You’re asking Node.js or Browser to show you something that needs to be converted into something visual. As the intended has a conversion factor each Javascript engine will treat it differently.

Node.js logs Strings, the browser allows pointers and therefore shows objects that can have new values since the console.log ran.

Node.js does not place id in quotes, but reacts to $ and puts this key inside quotes. If the object case was { id: { gte: 5 } } I wouldn’t put it in quotes either. These are variations between Javascript environments/engines.

How to prevent this?

Asks for the console.log show you something that is already converted into String. That is to say:

console.log(JSON.stringify({ id: { $gte: 5 } }));`

So you’ll have a homogeneous response, the same everywhere:

{"id":{"$gte":5}}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.