1

I don't understand what's happening here - in the below code I have an array of JSON objects. If I console log the array, and after the console log set a new property in one of the objects - the console log before this is showing the property.

How can this happen, if I'm doing logging the console before I assign the property?

It's causing a bigger issue for me, as I'm trying to set a property within a loop but clearing the array after a new action so the property isn't set for another event - but it just keeps setting the property and won't reset the array...

var sizes = [];

var sizes = [{title: 'S', id: 1}, {title: 'M', id: 2}, {title: 'L', id: 3}];

console.log(sizes);

sizes[1]['selected'] = true;
Ashley Banks
  • 528
  • 5
  • 16
  • 2
    Is this in Chrome? I'm pretty sure Dev Tools has a "live" feature, since the object logged to console is a reference. You could also do `console.log(JSON.stringify(sizes))` to solidify the structure at that point in time – Ian May 28 '14 at 13:28
  • 1
    @Ian That's true. I'd suggest posting that as an answer. – Ram May 28 '14 at 13:29
  • 1
    @undefined I'm pretty sure there's duplicates of this question that probably make more sense, I'll try to find one – Ian May 28 '14 at 13:30
  • 1
    @Ian, got it : http://stackoverflow.com/questions/7389069/console-log-object-at-current-state – Karl-André Gagnon May 28 '14 at 13:31
  • @Karl-AndréGagnon Nice, I was having trouble finding a SO post – Ian May 28 '14 at 13:32
  • I've solved my own issue by having a conditional statement to set the property to either true or false rather than relying on just setting it altogether, as you can't seem to. – Ashley Banks May 28 '14 at 13:43

0 Answers0