JavaScript Objects and for in loops [duplicate] - javascript

This question already has answers here:
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 8 years ago.
I'm working my way through a tutorial and I have hit a snag.
I created this object and I'm trying to loop through and print the first names of the entries. I thought it was working, but then I noticed that when I change 'firstName' in my for loop to 'number' or 'lastName', it still writes the first names to the console.
Can anyone shed some light for me? Thanks!
var friends = {
bill:{
firstName:"Bill",
lastName:"Gates",
number:"617-333-3333",
address:["One Microsoft Way", "Redmond", "WA", "98052"]
},
steve:{
firstName:"Steve",
lastName:"Jobbs",
number:"617-222-2222",
address:["15 Idontknow Street", "No Idea", "NO", "33333"]
}
};
var list = function(friends){
for (var firstName in friends) {
console.log(firstName);
}
}

The property names of your "friends" object are "bill" and "steve". That's what the loop iterates through — the property names. It doesn't matter what variable name you use.
What your loop is currently printing is "bill" and "steve", not "Bill" and "Steve". If you want to log the properties of the objects referenced by the properties of the "friends" object, you have to access those objects. If you wanted the "number" for example:
for (var fn in friends) {
console.log(friends[fn].number);
}

Related

Working with property accessors using string [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I refer to a variable using a string containing its name?
(7 answers)
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
Closed 9 months ago.
I am trying to access a property’s value in an object, I know the name of the properties, but not the name of the object (if that makes sense).
This object name is retrieved from an array. Sorry I’m not very good with explaining things, but hopefully an example will help…
So for example…
const names = ["john", "ben", "doe"]
const john = {
age: 33,
hobby: "coding"
};
const ben = {
age: 25,
hobby: "soccer"
};
const doe = {
age: 20,
hobby: "playing games"
};
names.forEach((name) => {
console.log(name.age)
console.log(name.hobby)
});
So as you can see, I know what properties each object has, the object name I also know, but it is being passed in an array of strings.
When I run this code, it will log undefined, how would I be able to retrieve the age and hobby of each of the values in the array?

Object complexity while search by key [duplicate]

This question already has answers here:
Complexity of accessing data in an object
(2 answers)
Closed 1 year ago.
I have the following object:
let obj = {
"name": "someName",
"age": 33,
"city": "someCity"
}
I understand that an object is not indexed
"JS objects have no defined order, they are (by definition) an
unsorted set of key-value pairs."
so what is the complexity of:
let result = obj.name
Is it searching all over the object to find the right key?
Is there a way to index this object so complexity will be O(1)?
Data access which involves retrieving or modifying data stored in an object is done in constant time O(1). This is also true for the insertion and removal of data.

Nested Objects and function variables [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I am trying to access a nested object based on the name of the object
I have already tried what I did below but it is giving me an error saying that "Cannot read property 'amount' of undefined" Can someone please explain to me why this doesn't work to point me in the direction of an alternative method? Thanks
var test = {
"name": {
"amount": "200"
},
"telle": {
"amount": "150"
}
}
function getIt(testing) {
return test.testing.amount;
//return test.name.amount;
}
console.log(getIt("name"))
function getIt(testing) {
return test[testing].amount;
}
That way you tell js to look for key with name stored inside testing variable.

Array of an already defined object in Javascript [duplicate]

This question already has an answer here:
creating arrays of objects in javascript
(1 answer)
Closed 5 years ago.
I have an object defined as
myObject = {
property1 : "",
property2 : ""
}
I need to create an Array of the above object.
I have tried using
myArray = myObject[].
But it doesn't seem to work.Is there any way that we can create an array of a predefined object in javascript.
Is there any way that we can create an array of a predefined object in javascript?
No, in JavaScript, you can't type the content of your array at the declaration of your array.
What you are looking for is TypeScript.

Array updating before push - javascript [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
JavaScript console prints assigned value of variable before it has been assigned?
(8 answers)
Closed 9 years ago.
I've got a curious situation where I am trying to update an array of objects with a new object, but when I place a console.log statement before the push, it shows that the array already has the new object inside of it. Here is the basics of the code:
var array1=[{
"Name": "Lake",
"ID": "1234"
}];
var object1={
"Name": "Mountain",
"ID": "1234"
};
function testArray() {
console.log(array1);
array1.push(object1);
}
I'd eventually like to update the original array with new information if the object contains the same ID. If it does not contain the same ID, then it should be appended. This would happen with an $.each loop over array1.
I'd greatly appreciate any help. Thank you.
It is because you are doing this in a webkit broswer like Chrome and console.log() is being queued up (it's a webkit bug, it won't happen if you do it in Firefox or non-webkit broswer), and therefore it prints a later value of the array. You need to use
JSON.stringify(array1);
for a more accurate result.
If you want to update the original array with new information only when the object contains the same ID, use an if statement to check the ID:
function updateA(obj){
if(obj.ID === array1.ID){
array1.push(obj);
console.log( JSON.stringify(array1));
}
}
updateA(object1);

Categories