How is possible show JSON value with different way [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
I wonder how it is possible in a similar situation, the solution
var lang_object = {
"UK": {
"ERROR": {
"fullname_empty": "fullname error",
"phone_empty": "phone error",
}
}
};
I have JSON Object.
var z = 'UK';
console.log(lang_object.z.ERROR.fullname_empty);
this example do not work, why? z = "UK".
var z = eval('UK');
console.log(lang_object.z.ERROR.fullname_empty);
this also dont work.
console.log(lang_object.UK.ERROR.fullname_empty);
this example work

Short answer:
lang_object[z].ERROR.fullname_empty
Long answer:
object.z refers to the value of the key z in the object object.
object[z] refers to the value of a key equals to the value z, in the object object.

Related

object name is same as variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 3 years ago.
Let's say I have an object
const myArray = {
a : "hello"
}
and I have a string with the same name of that object
like
var type ="myArray";
when I do console.log(type);
output: myArray
but I want to out that object to the console which has the same name as the value of variable type.
How should I do that?
Thanks in advance
If it's a global variable, it will be stored in window.
So, you can do something like console.log(window[type]) to access to value.

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.

How to use an object's value as the parameter for another object in Javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
If I have:
var key = "Section";
course.key = "101";
I get told course.key is unidentified. I'm trying to set course.Section = "101". Is there anyway to pass key's value in the second line?
The property key of course is undefined. Use bracket notation.
course[key] = "101"
is the same as
course["Section"] = "101"

Adding custom variables to an object [duplicate]

This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
Suppose I have an object obj which has some variables in it. What I want to do is to add a new variable which has a name i gave as a parameter.
For example:
var obj=
{
number:8
}
function AddField(field_name,obj)
{
//some magical code here
}
If I call
AddField('name',obj);
i want to be able to do
obj.name='Apple'
after calling this function.
Any ideas?
Use square brackets notation:
obj[field_name] = value;
REF: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId
Use the bracket notation:
obj[field_name] = void 0;
This creates a new property with an undefined value.
If obj is already defined, you can just do obj.field_name = null or obj["field_name"] = null.
I wouldn't set any value to undefined even though the language allows for that, simply for a semantic issue: undefined means "this property/variable has not been defined" and null means "this property/variable has no value".

How to refer to object fields with a variable? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
Let's asume that I have an object:
var obj = {"A":"a", "B":"b", "x":"y", "a":"b"}
When I want to refer to "A" I just write obj.A
How to do it when I have key in a variable, i.e.:
var key = "A";
Is there any function that returns a value or null (if key isn't in the object)?
Use bracket notation, like this:
var key = "A";
var value = json[key];
In JavaScript these two are equivalent:
object.Property
object["Property"];
And just to be clear, this isn't JSON specific, JSON is just a specific subset of object notation...this works on any JavaScript object. The result will be undefined if it's not in the object, you can try all of this here.
How about:
json[key]
Try:
json.hasOwnProperty(key)
for the second part of your question (see Checking if a key exists in a JavaScript object?)

Categories