So I have an array, where I am trying to update an objects property value, like so:
this.tasks[5].status = "complete";
// Here is the object looks like when I log it:
{"rowID":16,"task":"and more stuff","status":"incomplete","inlist":"Homework"}
Yet the above code does not change the value, it does nothing. Is my syntax wrong, or is there something else in my code causing this?
It seems I can add properties to the object, but I cant modify existing ones. I also can delete the object, but cant delete a property from it.
Thanks
It must be something else in your code. Here are two pics of your setup working in both Chrome and IE9.
Chrome Console
IE9 Console
Related
I have an object downloaded from firebase which is of type Bug (Custom type). However, when I tried to console log the output with the following code
console.log('bug',this.bug);
I get the following result. It is what I expected where I can go on and do things like
console.log('company', this.bug.companyName)
However, I want to get that key value as well. Howe can I do that? I tried to do
console.log('company', this.bug.key)
console.log('company', this.bug.$key)
and both of them does not work
try to run
console.log('company', this.bug["$key"])
remember property lookup on objects is either
obj.someKey // or
obj["someKey"]
I must be missing something pretty standard here.
I have a map mapping string to object. What I write out:
console.log(playerMap, this.id(), playerMap[this.id()]);
What it prints out:
this does not make a lot of sense to me since the key seems to be right there.
Fiddle
Based on your Fiddle, apparently your playerMap is not yet initialised when you new FeedViewModel(), if you change your console.log to
console.log(Object.keys(playerMap).length, this.id(), playerMap[this.id()]);
you will see the object is empty
> 0 "123243df6" undefined
It's always confused when you console.log object because the object displayed is only resolved when you expand the > in the console. It is not the state of the object when you console.log'd the object.
I have these lines:
console.log(JSON.stringify(this.model.attributes));
console.log(this.model.get('name'));
And this is the output:
"{"name":"ffg","key":"1c277f82-f093-d359-4cfb-febe4614a3b1"}"
""
I'm starting with Backbone.
Any ideas why the object is in the attributes but it returns empty with the get method??
EDIT:
This shouldn't affect answers, but I'm working with Phone Gap.
The 2 console.log are lines next to each other.
EDIT 2:
var temp = _(this.model.attributes).clone();
console.log(JSON.stringify(temp));
console.log(temp.name);
This logs:
{"name":"ss","questions":[],"order":0,"key":"5c35c304-4863-02c0-4d18-101c655aa4ae"}
""
I would consider logging this.model and inspecting the object into a browser (e.g. Chrome or Firefox) then look into the attributes property of the object. Make sure that the name property is defined as you intended. Also, please make sure that no event is triggered that could modify your model object.
When you fetch model, try set this yourModel.fetch({async: false})
I am using JSON.parse to parse this JSON string
[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]
However I am simply getting this result as the output:
[object Object]
Which shouldn't be the result. I am using this within the Cappuccino framework. Does anyone know what I am doing wrong here?
[object Object] is what objects display when you call toString on them. It looks like you're taking your result and trying to call obj.toString()
Also, your JSON is an array with one element in it, so to verify that your result is correct, you can access the name property on the [0] index:
obj[0].name // should be "joe".
var text = '[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]';
var obj = JSON.parse(text);
alert(obj[0].name); //alerts joe
DEMO
Or get rid of the array, since it's not really doing much
var text = '{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}';
var obj = JSON.parse(text);
alert(obj.name); //still joe
DEMO
This is an array because it's in square brackets - [] - remove these and it should work...
Even though this is 'syntactically' correct the parser sees this as an array (which is a type of object) but won't do the work on it the way you'd expect.
Also for future reference:
Try to lint it, and see if your syntax is messed up: http://jsonlint.com/
This is an old subject, but nonetheless, I spent hours trying to figure out what was going on. So, hopefully this will help someone else in the future.
My issue was setting up a simple ajax call, and doing something with the resultset from said ajax call. However, no matter what I did, I couldn't get the json resultset to an object.
I ended up stepping through everything in the debugger window and noticed old code that was no longer active was showing up on the sidebar (dom detail). So, my data had been cached. I cleared the cache, and boom! Everything worked.
What I'm given in my homework is and JS object that looks like:
myObj =
{name:eric, location:belgium, age:24},
{name:jools, location:holland, age26},
{name:mike, location:usa, age:30},
the idea is that somehow if i need to target 'location' holland i need to be able to treat all this like an arary so I can work with indexes (at least that's what I think). I Can't find any example anywhere where people work with this been searching for a bit on 'js object'.
The actual challenge is to be able to put the different values of the 'name' property as innerHTML(or some method that does something similar) of new option elements inside a given select element probably through a loop. Since this is homework, I don't need the actual code for that but a clue on where I can learn more about how these JS object property array type of things work would be nice.
thanks a lot!
Your JavaScript snippet is invalid, something makes me think there was a copy-and-paste error. The answer changes significantly depending on what the code actually looks like.
If it looks like this:
myObj = [
{name:eric, location:belgium, age:24},
{name:jools, location:holland, age26},
{name:mike, location:usa, age:30},
// ...
];
...then you're dealing with an array of objects, where each object has the properties name and location. You can loop through them using a standard for loop with an index variable, counting from 0 to myObj.length - 1 (inclusive), and access the properties of each object via myObj[index].name and myObj[index].location.