2

I have an object then have another objects as values. And that objects have arrays as values. And I need to modify each that array. Here is 2 items of my object:

 '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 ] },

So I need to divide every item in the array on the first item, and then multiply third item on 100 and round all the items.

I have tried to do it with underscore map like so:

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?
});

But I don;t know what should I return in the map function so my var would be an object.

What is the right way to do that?

3
  • can you use jquery? I think i can fix it but i prefer jquery than underscore Commented Sep 19, 2014 at 8:59
  • this is server-side application that runs on node Commented Sep 19, 2014 at 9:00
  • ah ok, i can do it with pure js anyway Commented Sep 19, 2014 at 9:05

2 Answers 2

2

A solution is not using _.map but just run over your collection changing the values on the go with _.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/

Sign up to request clarification or add additional context in comments.

2 Comments

fiddle gave me Uncaught TypeError: undefined is not a function
sure you selected underscore extension? It is working fine for me
1

This works on my fiddle:

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.