How do I get the value of a # object attribute? [duplicate] - javascript

This question already has answers here:
How to access object property with invalid characters
(2 answers)
Closed 7 years ago.
So I'm getting some JSON data containing 12 objects. outputting the objects to console shows me:
Object{ (...) }
#categoryID: "123"
#id: "234"
categoryTitle: "abc"
(...)
If I want to fetch the category title, I simply do item.categoryTitle. But if I want the ID, I can't use item.id.
According to this answer, one can use $object->{'#id'};, but trying $item->{'#id'} is not working.
So how to I get this value?

Refer it using ['key'] syntax
var obj = { '#id' : 123 };
console.log(obj['#id']); //123

Related

Undefined as value from Json with Javascript [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
{
pet: {
"0.628": 92694.5,
"8739.836": 96391.94
},
try: {
//same
}
}
When I specify the key, I get the values but i am trying to read all the values without knowing the keys. I have even tried regex, but nothing seems to be working. As you can see i am fairly new. So sorry if this was a stupid question.
console.log(data.pet) // Gives [Object Object]
console.log(data.pet["0.628"])//Gives the value
console.log(data.pet[0])//Gives undefined
I don't see in what context you'd want to access a json object without knowing the keys.
but what you can do is to parse the json file into a javascript object, and call Object.keys() to get the keys of that object

Find specific item in an array? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I have an array that looks like this:
cardNumber: "2344234454562036"
cardType: "Visa"
cardholderName: "Yeyy"
cvv: "123"
expiryMonth: 12
expiryYear: 2022
postalCode: "52636"
redactedCardNumber: "••••••••••••2036"
__proto__: —'
I need to get the value of redactedCardNumber from that array.
The first thing that I am confused about is that when i print the above 'array' in the console, it actually show an object before everything! so is this an array or an object?
also, what is the best way of getting the specific item like 'redactedCardNumber' from it?
It looks like an object. If so, you can access it by obj.redactedCardNumber.

Get object element from a defined string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.

Add a new attribute to json object [duplicate]

This question already has answers here:
Add new attribute (element) to JSON object using JavaScript
(11 answers)
Add property to each object in the array [duplicate]
(2 answers)
Closed 6 years ago.
I have a JSON object of the following format
[{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"},
{......},
{......},
]
I need to add a new attribute to each row based on some calculations.
Expected result
[{"field1":1,"field2":"A","field3":"B","field4"="generatedVal1"},
{"field1":2,"field2":"B","field3":"C","field4"="generatedVal2"},
{......},
{......},
]
How can I achieve this using javascript?
Use Array.prototype.forEach method:
[
{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"}
]
.forEach(obj => {
obj.field4 = 'Something'
})
Sidenote on terminology: you don't have any JSON, you have javascript array (object). JSON is a string representation of this object, but in your question you are talking about object itself.

How to get the value dynamically from JSON object [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 7 years ago.
I want to retrieve a JSON value dynamically from JSON object. Below is the code i am using to get the value from JSON object.
var jsonObj = JSON.parse(jsonData);
console.log(jsonObj);
jsonSplit = jsonToFind.split(htmlSplit+".")[1].trim();
console.log(jsonObj+"."+jsonSplit);
But I am getting [object Object].ensighten_tag.
Here ensighten_tag is the dynamic key value.
Can anyone suggest me how to get the value dynamically ?
console.log(JsonObj[jsonSplit])

Categories