This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 10 years ago.
I have a javascript issue.
If I have an object array objAr, the object consists of id,name.
If I was to access objAr[0].id it returns the id value of the first object. What would happen if the object is dynamic and therefore I do not know what it consists of, is there a way to dynamically call the Object attribute?
Currently I am creating another array
var theArr = new Array("id", "name");
and call:
objAr[0].theArr[0] instead of objAr[0].id.
Is there a way to do this better using Javascript?
With Javascript you can call all of the attributes in an object without knowing the keys.
See below:
for(key in objAr[0]) {
console.log(objAr[0][key]);
}
If you just wanted the first attribute you could run:
for(key in objAr[0]) {
var attFirst = objAr[0][key];
break;
}
Additionally for the JS array you could have used square brackets.
var theArr = ["id", "name"];
hope that helps
In javascript you can always use the "array notation" in place of the "dot notation"
So these 2 lines are the same
objAr[0].id
objAr[0]["id"]
Related
This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 5 months ago.
There is a way to pass a string in a function to other function and this string is a reference to an object in this other function?
function fun1 (structure){
obj = [{"header":{"orderNumber": a, "item": 1}}, {"header":{"orderNumber": b, "item": 1}}]
newObj = obj[0].structure
console.log(newObj)
//first call a
//second call 1
}
//first call
function fun2(){
structure = 'header.orderNumber'
fun1(structure)
}
//second call
fun2(){
structure = 'header.item'
fun1(structure)
}
What I want is a dynamic way to access an object by creating a string.
For example in the block of code obj.header.item.description and this is valid. I would like to pass a string in this string somehow make a reference to the object so I can get the value.
One way you could accomplish this would be by storing all of the object references in a Map (MDN) structure where the key is a string and the value is the object reference in question.
I should say that even though I don't know your exact use case, this is what you would call a "stringly typed" solution which is generally not the best idea.
This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
for example I ask the user to enter an input and he enters "key" and I save the value on a variable called INPUT.
Then I want to create an object with this like this.
var obj = {INPUT: "some other input"}; where INPUT = key
I know I can add more values but I need to know the key ahead of time in order to add it. Can I add a new key without knowing what it is?
Yes, with computed property names. Assuming INPUT is an actual variable, simply wrap it in [].
var obj = {[INPUT]: "some other input"};
The long version of doing this would be to use bracket notation when adding your keys.
var obj = {};
obj[INPUT] = "some other input";
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I get the values of the first or NAMED key in the object. This one is called Efficacy.
var PSEQ_Obj = $.parseJSON( PSEQ );
var PSEQ_dps1 = PSEQ_Obj.Efficacy;
But I want to know if there is a dynamic way to do this.
For instance. Bellow will return Efficacy.
for (label in PSEQ_Obj) break;
But I cant use it lik follows
var PSEQ_dps1 = PSEQ_Obj.label;
So this label is a dynamic, but you cant call it exactly like above.
Is there a different way?
you have to use bracket notation instead of dot notation when accessing properties that are stored in variables as follows :
var PSEQ_dps1 = PSEQ_Obj[label];
you can read more about this topic on MDN.
This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 8 years ago.
I have a question about referencing objects in javascript.
Say I have a variable that is some object (lets say json) and it is called objOne - (var objOne = someJSONObject;).
If I go ahead and declare
var objTwo = objOne;
will I have two references to the same Object? Kind of like a c pointer?
To sum it up :
assignements are done by value
you never manipulate objects, only object references
This means that
you'll have two references to the same object (you can check that by changing a property of the object)
when you're passed in a variable the value of a primitive, changing your variable doesn't change other variables
EDIT : as it's a duplicate I'll delete this answer in a minute to allow a proper closing if there's no other answer. Please vote to close.
Yes, objects are passed by reference:
function changeVal(obj){
obj.value = "bar"
}
(function checkRefs(){
var myObject = {
value: "foo"
};
alert(myObject.value);
changeVal(myObject);
alert(myObject.value);
})();