简体   繁体   中英

underscore not retaining keys on nested object

I have an object that looks like this:

var ingredientsObject = {
    "Ingredients": [
        { "Section": "Ingredienser", "Name": "salt", "Value": 1, "Unit": "tsk" },
        { "Section": "Ingredienser", "Name": "olivolja", "Value": 1, "Unit": "msk" },
        { "Section": "Ingredienser", "Name": "lasagneplattor, (125 g) färska", "Value": 6, "Unit": "st" },
        { "Section": "Tomatsås", "Name": "salt", "Value": 0.5, "Unit": "tsk" },
        { "Section": "Tomatsås", "Name": "strösocker", "Value": 2, "Unit": "krm" }
        { "Section": "Béchamelsås", "Name": "salt", "Value": 0.5, "Unit": "tsk" },
        { "Section": "Béchamelsås", "Name": "smör", "Value": 2.5, "Unit": "msk" }
    ]
};

and I am trying to recalculate the value of each ingredient based on the number of servings specified using underscore.

I have tried using mapObject ( http://underscorejs.org/#mapObject ):

newIngredients = _.mapObject(ingredients.Ingredients, function (val, key) {
    return val.Value / modifier;
});

but that returns an object that looks like this:

Object {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 2, 3: 0.3333333333333333, 4: 50, 5: 66.66666666666667, 6: 0.16666666666666666, 7: 0.6666666666666666, 8: 0.25, 9: 0.3333333333333333, 10: 0.3333333333333333, 11: 0.16666666666666666, 12: 0.8333333333333334, 13: 0.16666666666666666, 14: 0.8333333333333334, 15: 1.6666666666666667, 16: 0.6666666666666666}

whereas what I actually want is the original object with only the values changed, as in:

var ingredientsObject = {
    "Ingredients": [
        { "Section": "Ingredienser", "Name": "salt", "Value": 0.3333333333333333, "Unit": "tsk" },
        { "Section": "Ingredienser", "Name": "olivolja", "Value": 0.3333333333333333, "Unit": "msk" },
        { "Section": "Ingredienser", "Name": "lasagneplattor, (125 g) färska", "Value": 2, "Unit": "st" }
        // and so on...
    ]
};

How do I achieve this?

Try:

 newIngredients = _.map(ingredientsObject.Ingredients, function(item) {
    return { 
      Section: item.Section,
      Name: item.Name,
      Value: item.Value / modifier,
      Unit: item.Unit
    };
});

Actually ingredients.Ingredients is an array, _.mapObejct expect a object as the first param. You can do this in underscore way:

_.mapObject(ingredientsObject, function(val, key) {
    return _.map(val, function(ingredient) {
        return _.extend(
          {},
          ingredient,
          {
            Value: ingredient.Value / modifier
          }
        )
    })
})

Ok, based on the comments and suggestions I received, I came up with this solution:

newIngredients = _.each(ingredientsObject, function (list) {
    _.each(list, function (item) {
        item.Value = item.Value / modifier;
    });
});

This modifies the value itself without modifying the object structure.

Thanks @nnnnnn for pointing me in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM