cristodagama cristodagama - 2 years ago 105
Javascript Question

Eloquent way to use a javascript object instead of multiple arguments for a funciton, but still can input the arguments?

I know the pattern that basically tells you to create a js object and then pass in that object as a param into the parent function, but I have a slight twist on this that I wanted to see if was possible.

Say I have the following parent function

//helper functions
function handleArray(array, target, n) {
for (var i = 0; i < n; i++) {
array.push(new target());

//parent function

function tally(n1, n2, n3, n4, n5, cargo) {
var sportArray = [];
var familyArray = [];
var truckArray = [];
var miniArray = [];
var cargoArray = [];
var output =[];
var parsed = [];
var names = [];

//buids out my objects based on helper function above
handleArray(sportArray,SportsCar, n1);
handleArray(familyArray,FamilyCar, n2);
handleArray(truckArray,Truck, n3);
handleArray(miniArray,MiniVan, n4);
handleArray(cargoArray,CargoVan, n5);

//storing all the cars together in mega array
var carArray = sportArray.concat(familyArray, truckArray, miniArray, cargoArray);

//gives us a mapped array of just the capacity values stored in one neat array
var carsMapped = function(o) { return o.capacity; });

var startCargo = cargo; //maintain original in memory

var sumTotal = carsMapped.reduce(function(prev,curr){
return prev + curr;

// strggling hard core here maybe revise OO properties to make easier?
// var test = full(carsMapped, cargo);

// console.log();

//now for the printing finale!!!!
console.log("Allocating " + startCargo + " LB of cargo in total");

for(var i = 0; i < carArray.length; i++) {
console.log( "A " + names[i] + " with " + parsed[i] + " LBs");
console.log("We have " + cargo + " LBs of cargo left over.")

// //test output
tally(1,3,4,2,1, 7356);
// tally(1, 6, 1, 2, 3, 7356);

// // tally(1,1,1,1,1);
// // tally(0,0,1,0,0);

I want to be able to condense n1..n5 arguments into ideally just two arguments. However if I understand the options object param it would be something like this:

var options = {
sport: 'n1',
family: 'n2',
truck: n3,
mini: n4,
cargo: n5

function tally(options, cargo){...;}

This is fine except I cant actuallly set the values of n1..n5 like before hand and this is critical to my parent function tally to work because n1..n5 represent different objects that are created on the fly.

Any patterns or tips on how I could use less arguments while working around these issues?


Answer Source

Firstly, you had the order of the object's values backwards. It should be key: value, not the other way around. You use an object as the parameter when you call the method, but it doesn't have to be create beforehand, it can be done in the parameters like this.

tally ({n1: 'sport', n2: 'family', n3: 'truck', n4: 'mini', n5: 'cargo'});

Or, if you prefer making an object beforehand:

var options = {
    n1: 'sport',
    n2: 'family',
    n3: 'truck',
    n4: 'mini',
    n5: 'cargo'

tally (options);

Where the function would be written as follows: You can easily access the object's values by doing object.key (where the keys are, in this case, n1-n5, and the object is data).

function tally (data) {
    var n1 = data.n1; //in this example, n1 = 'sport'
    var n2 = data.n2; //and n2 = 'family'
    //Do stuff

If you wanted the first parameter value to be 4, you could do:

tally ({n1: 4, ...})

and if you wanted 10, you could do:

tally ({n1: 10, ...})

or if you just wanted it to match a previous variable x, you could do:

tally ({n1: x, ...})

EDIT: From your edited question code, you would do the following (only showing changed code for readability):

function tally(data, cargo) {
  var sportArray = [];
  var familyArray = [];
  var truckArray = [];
  var miniArray = [];
  var cargoArray = [];
  var output =[];
  var parsed = [];
  var names = [];

handleArray(sportArray,SportsCar, data.n1);
handleArray(familyArray,FamilyCar, data.n2);
handleArray(truckArray,Truck, data.n3);
handleArray(miniArray,MiniVan, data.n4);
handleArray(cargoArray,CargoVan, data.n5);

//Unchanged code removed

//test output
tally({n1: 1, n2: 3, n3: 4, n4: 2, n5: 1}, 7356);
tally({n1: 1, n2: 6, n3: 1, n4: 2, n5: 3}, 7356);
tally({n1: 1, n2: 1, n3: 1, n4: 1, n5: 1}, 1);
tally({n1: 0, n2: 0, n3: 1, n4: 0, n5: 0}, 0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download