fpena06
fpena06

Reputation: 2426

underscore each wrong values?

Can someone please help me understand why I'm not getting the expected output. I have the following code.

var targetArray = [];
var tempVar = {};
var json = [
    { id:1, available:false, value:'vodka', quantity:1 },
    { id:2, available:true, value:'WINE', quantity:13 },
    { id:2, available:true, value:'WINE', quantity:13 },
    { id:3, available:true, value:'gin', quantity:42 },
    { id:4, available:true, value:'scotch', quantity:21 },
    { id:5, available:true, value:'WHISKEY', quantity:123 },
    { id:100, available:true, value:'schnapps', quantity:5 },
    { id:120, available:false, value:'beer', quantity:0 },
    { id:2, available:true, value:'WINE', quantity:13 },
    { id:999, available:true, value:'rum', quantity:55 },
    { id:1, available:false, value:'Vodka', quantity:1 },
    { id:13, available:true, value:'brandy', quantity:3 },
    { id:42, available:true, value:'Tequila', quantity:88 }
];

_.each(json, function(value){
    tempVar.value = value.value;
    tempVar.available = value.available;
    targetArray.push(tempVar);
  });

alert(JSON.stringify(targetArray));

My output is the following.

[{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true},{"value":"Tequila","available":true}]

When I try this it alerts me each value like it's supposed to.

_.each(json, function(value){
    alert(JSON.stringify(value.value));
});

Am I missing something?

here is the fiddle

http://jsfiddle.net/a6Rx4/421/

Thanks in advance.

Upvotes: 0

Views: 57

Answers (1)

xdazz
xdazz

Reputation: 160833

You are pushing the same object. Instead, create a new object each time like below:

_.each(json, function(value){
    targetArray.push({value: value.value, available: value.available });
});

And for such case, you could also use .map method:

var json = [
    { id:1, available:false, value:'vodka', quantity:1 },
    { id:2, available:true, value:'WINE', quantity:13 },
    { id:2, available:true, value:'WINE', quantity:13 },
    { id:3, available:true, value:'gin', quantity:42 },
    { id:4, available:true, value:'scotch', quantity:21 },
    { id:5, available:true, value:'WHISKEY', quantity:123 },
    { id:100, available:true, value:'schnapps', quantity:5 },
    { id:120, available:false, value:'beer', quantity:0 },
    { id:2, available:true, value:'WINE', quantity:13 },
    { id:999, available:true, value:'rum', quantity:55 },
    { id:1, available:false, value:'Vodka', quantity:1 },
    { id:13, available:true, value:'brandy', quantity:3 },
    { id:42, available:true, value:'Tequila', quantity:88 }
];

var targetArray = json.map(function (e) {
  return { value: e.value, available: e.available };
});

Upvotes: 3

Related Questions