Printing out the properties of an object [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm following Code Academy's Javascript tutorial. Here's the lesson, I'm having trouble with:
Use a for-in loop to print out all the properties of nyc.
Here's my code:
var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};
for(var x in nyc) {
console.log(nyc[x]);
}
When I execute this, Code Academy gives me the following error:
Oops, try again. It looks like you didn't print nyc's fullName
I used Chrome's developer tools to run this code and I seem to be getting the appropriate output:
New York City
Bill de Blasio
8000000
5
What am I doing wrong here?
Edit: I took Pointy's advice on changing the inside of the for-in loop to console.log(x). I still got the same error until I closed the Code Academy tab and opened up a new one.

x is your key to get the value you use the key to access it inside the object nyc as in
for(var x in nyc) {
console.log(nyc[x]);
}

for(var x in nyc) {
// x refers to the current property name.
console.log(x);
// To look up the *value* of that property, use the [] notation
console.log(nyc[x]);
}

Related

Return object result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 26 days ago.
Improve this question
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)']);
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.

Cannot read property 'teacherId' of undefined when calling the array of object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
var teacherArray=[];
I have created an array variable.
I am creating an array with the key and value. and pushing these data to the teacherArray.
random={
teacherId:TeacherId,
day:day,
periodCount:period,
class:Studentclass,
section:Studentsection,
startTime:schoolStartTime,
endTime:schoolEndTime
};
teacherArray.push(random);
console.log(teacherArray);
In the console I am able to see the created array. But when the submit button clicked i am calling the array like
teacherLength=teacherArray.length;
for (let k=0;k<=teacherLength;k++)
{
var teachId=teacherArray[k].teacherId;
console.log(teachId);
}
In the console displays the teacherId, but next line shows the error as
TypeError: Cannot read property 'teacherId' of undefined
Arrays are zero index based. So when you write k<=teacherLength you are requesting more than what array have. That should be changed to
k<teacherLength
Arrays are 0 indexed. You are having,
for (let k=0;k<=teacherLength;k++)
Make it,
for (let k=0;k<teacherLength;k++)
Your code is reading an extra item, you need to replace
for (let k=0;k<=teacherLength;k++)
with
for (let k=0;k<teacherLength;k++) //notice < instead of <=

Need Array inside other Array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
For some reason I need to push an Array inside other array.
For example,
var a = ["Test1", 1];
var b = ["Test2", 2];
var c = [];
c.push(a);
c.push(b);
alert(c);
For this code I need the following output,
["Test1", 1],["Test2", 2]
But what I am getting is
Test1,1,Test2,2
Any help will be highly appreciable.
You've already done it correctly: c.push(a) and c.push(b) work, but you don't want to use alert() for debugging.
Though it might be more convenient since you don't have to open up a console, it's going to give you output that is inconsistent with the actual structure of the data because using alert(x) converts whatever x is to a string.
Always use console.log(). Had you done that in this case, you would have seen something like this in the console:
Demo

Defining String Arrarys [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Hi I'm a beginner in Javascript and somethinh strange occured.
I wanted to create an global String array in an external Javascript file with
var natArray = new Array(20); But my IDE(Webstorm 7) Says me newArray is an Unresolved function and I cant use the array in another function it was created for(Source: Javascript Console)
Thank you for your time and feedback.
EDIT: Rest of the Code
function country(name){
for(i=0;i<20;i++)
natArray[i]=name;
document.write(natArray[i]);
}
And here i call it(a html file):
Country:
<select>
<script type="text/javascript">
country("RandomCountry1");
country("RandomCountry2");
</script>
</select>
Im just getting an empty drop-down-list
I'm not sure what you are trying to accomplish, but the reason that document.write(natArray[i]); is returning undefined is that you are leaking i to the global scope in for(i=0;i<20;i++).
Once this loop has completed, i is equal to 20, even outside the scope of the loop (to avoid this, try for(var i=0;i<20;i++). Although natArray has a .length of 20, arrays elements are zero-indexed, meaning that they start counting at 0, not 1.
Because of this, if what you are trying to do is access the last element of natArray, you can/should do so with natArray[natArray.length - 1]. The -1 accounts for the zero-indexed nature of Javascript arrays. If natArray has a length of 20, you will fail to access natArray[20] because this statement is actually trying to access the 21st element, not the 20th.

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources