使用Underscore修改另一个对象内的对象内的数组?

时间:2014-09-19 08:43:28

标签: javascript arrays underscore.js

我有一个对象然后有另一个对象作为值。而且这些对象将数组作为值。我需要修改每个数组。这是我的两件物品:

 'Toyota Prius hybrid hatchback':
  { '2001': [ 2, 8.2, 2, 10, 8, 8 ],
    '2002': [ 2, 7.6, 1, 9, 6, 6 ],
    '2004': [ 10, 45.900000000000006, 7, 50, 43, 43 ],
    '2005': [ 17, 75.1, 11, 82, 62, 78 ],
    '2006': [ 27, 119.6, 17, 133, 106, 120 ],
    '2007': [ 20, 84.70000000000002, 17, 99, 87, 92 ],
    '2008': [ 33, 139.49999999999997, 17, 154, 124, 141 ],
    '2009': [ 66, 292.40000000000003, 49, 322, 273, 298 ],
    '2010': [ 58, 259.0999999999999, 39, 281, 240, 263 ],
    '2011': [ 22, 96.50000000000001, 15, 107, 90, 101 ],
    '2012': [ 23, 104.70000000000002, 20, 114, 92, 105 ] },
 'Toyota RAV4 SUV':
  { '2001': [ 1, 3.9, 0, 4, 4, 3 ],
    '2002': [ 7, 27.4, 5, 28, 27, 23 ],
    '2003': [ 7, 28.900000000000002, 4, 35, 27, 26 ],
    '2004': [ 2, 7.3, 1, 9, 7, 5 ],
    '2005': [ 6, 24.200000000000003, 5, 29, 25, 19 ],
    '2006': [ 11, 45.6, 10, 50, 46, 39 ],
    '2007': [ 9, 38.599999999999994, 7, 45, 35, 34 ],
    '2008': [ 10, 43.1, 8, 47, 43, 39 ],
    '2009': [ 11, 47.5, 8, 54, 48, 42 ],
    '2010': [ 7, 31.5, 5, 35, 32, 28 ],
    '2011': [ 11, 47, 7, 54, 47, 41 ],
    '2012': [ 11, 51.3, 9, 55, 51, 47 ] },

所以我需要在第一个项目上划分数组中的每个项目,然后将第三个项目乘以100并围绕所有项目。

我试图用下划线地图这样做:

var newObj = _.map(myObj, function(value, key){
    var b = _.map(value, function(value, key){
        var rev = value[0];
        var rat = (value[1]/rev).toFixed(1);
        var rec = (value[2]/rev*100).toFixed(0);
        var per1 = (value[3]/rev).toFixed(1);
        var per2 = (value[4]/rev).toFixed(1);
        var per3 = (value[5]/rev).toFixed(1);
        // What should I return so my b variable would be an object?
    });
    // What should I return so my newObj variable would be an object?
});

但是我不知道我应该在map函数中返回什么,所以我的var将是一个对象。

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

这适用于我的小提琴:

var cars ={
'Toyota Prius hybrid hatchback':
  { '2001': [ 2, 8.2, 2, 10, 8, 8 ],
    '2002': [ 2, 7.6, 1, 9, 6, 6 ],
    '2004': [ 10, 45.900000000000006, 7, 50, 43, 43 ],
    '2005': [ 17, 75.1, 11, 82, 62, 78 ],
    '2006': [ 27, 119.6, 17, 133, 106, 120 ],
    '2007': [ 20, 84.70000000000002, 17, 99, 87, 92 ],
    '2008': [ 33, 139.49999999999997, 17, 154, 124, 141 ],
    '2009': [ 66, 292.40000000000003, 49, 322, 273, 298 ],
    '2010': [ 58, 259.0999999999999, 39, 281, 240, 263 ],
    '2011': [ 22, 96.50000000000001, 15, 107, 90, 101 ],
    '2012': [ 23, 104.70000000000002, 20, 114, 92, 105 ] },
 'Toyota RAV4 SUV':
  { '2001': [ 1, 3.9, 0, 4, 4, 3 ],
    '2002': [ 7, 27.4, 5, 28, 27, 23 ],
    '2003': [ 7, 28.900000000000002, 4, 35, 27, 26 ],
    '2004': [ 2, 7.3, 1, 9, 7, 5 ],
    '2005': [ 6, 24.200000000000003, 5, 29, 25, 19 ],
    '2006': [ 11, 45.6, 10, 50, 46, 39 ],
    '2007': [ 9, 38.599999999999994, 7, 45, 35, 34 ],
    '2008': [ 10, 43.1, 8, 47, 43, 39 ],
    '2009': [ 11, 47.5, 8, 54, 48, 42 ],
    '2010': [ 7, 31.5, 5, 35, 32, 28 ],
    '2011': [ 11, 47, 7, 54, 47, 41 ],
    '2012': [ 11, 51.3, 9, 55, 51, 47 ] 
  }    
}    

for (var i = 0;i< Object.keys(cars).length; i++)
{
    var car = cars[Object.keys(cars)[i]];
    for (var j = 0; j < Object.keys(car).length; j ++)
    {
         var year = car[Object.keys(car)[j]];
         processArray(year);       
    }
}


function processArray(arr)
{    
    for (var i=1;i<arr.length; i++)
    {
        arr[i] = Math.round(arr[i] / arr[0] * 100);
    }    
}

http://jsfiddle.net/7hjLznym/3/

答案 1 :(得分:1)

解决方案不是使用_.map,而只是运行您的集合,使用_.each更改移动值。

_.each(myObj, function(item, key){
     _.each(item, function(value, key){
        var rev = value[0];
        value[1] = (value[1]/rev).toFixed(1);
        value[2] = (value[2]/rev*100).toFixed(0);
        value[3] = (value[3]/rev).toFixed(1);
        value[4] = (value[4]/rev).toFixed(1);
        value[5] = (value[5]/rev).toFixed(1);
    });
});

http://jsfiddle.net/0k96v1ts/2/