Return object result

I need to get the result of this object, I’ve tried json.count(id_reported) and json['count(id_reported)'] but none worked.

const json = { 
  'count(id_reported)': 21 
};
    
//console.log(json.count(id_reported));
console.log(json['count(id_reported)']);

>Solution :

In Javascript, Typescript and so in Express in the end, its easy to handle such things.

var myObject = { 
  'count': 21 
}

myObject = JSON.parse(myObject);
console.log(myObject.count);

The JSON.parse is only needed, if you object is a string. Is it a Javascript object you do not need to parse.

The count(id_reported) part I don’t understand. If your object looks like this in the end:

{
  count(1): 1,
  count(2): 2,
}

and you don’t know the structure at all you can use a for loop:

for (let data in myObject) {
  console.log(data); // data will be the key; so count(1) as example
}

See the in keyword in the for loop. This will give you the key. The on keyword otherwise gives the object in an array as example.

Leave a Reply