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 can't seem to find the bug that's making the code run twice, and JSFiddle isn't working for me so I can't double check if it's the editor.
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: 9,
address: ["5242", "drank avenue"]
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: 8,
address: ["3368", "pool lane"]
};
var list = function(person)
{
for (var friendName in person)
console.log(friendName);
};
list(friends);
Output:
bill
steve
bill
steve
I cut and pasted the code you posted into my JavaScript console and the result was:
bill
steve
Somewhere, in your original code, you must be calling the function twice.
Choose a place in the code — in this case I would choose the top of your list function — and put a
debugger;
statement there, and open the JavaScript console.
There will be an option in that debugger to see a stack trace. The stack trace will let you see exactly where a function was called from, so you can see if it is being called from two different points.
If that doesn't work, move the debugger statement to right above this line:
friends.bill = {
and repeat until you find the problem.
Related
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.
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 4 years ago.
Improve this question
the syntax seems to be correct, I dont get it, here is
error: app.js:73 Uncaught ReferenceError: getBirthyear is not defined
Code:
const person = {
name: 'Tommy',
age: 32,
location: {
state: 'Missouri',
city: 'louisisana',
street: '1 marcia drive',
job: 'web dev'
},
getBirthyear: function() {
return 2018 - this.age;
}
}
let val;
val = getBirthyear();
document.write(val);
You can call it by using person.getBirthday() since it is a property of person
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 6 years ago.
Improve this question
var data = {};
data.info.id = "alpha";
This logs to the console: "TypeError: data.info is undefined".
Well that's great and all but I need to store a value in data.info.id. Isn't that what objects are supposed to do?
This should produce an object that looks like this:
data: {
info: {
id: "alpha"
}
}
Is data.info = {} really a necessary step?
In response to Patrick Evans - that's an unrelated question.
Well there is another way. That's putting the info-object directly in the data-object like this:
var data = {
info: {}
}
data.info.id = "alpha";
console.log(data);
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]);
}
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.