lukechambers91
lukechambers91

Reputation: 711

Javascript function optional parameters

I have a simple function that updates the values of an object. Currently, the function takes in 3 parameters to updated the object. I would like an optional fourth parameter that will accept an array of objects (array) and change the values for all the objects in the array. If the fourth parameter is provided I would like to ignore the others. What is the best way to do this? Thanks

var myObj = {myKey : 0}
var obj1 = {myKey : 0}
var obj2 = {myKey : 0}
var array = [{obj: obj1, key: 'myKey1', value: 1},{obj: obj2, key: 'myKey2', value: 2}]

function changeRule(obj, key, value) {
        obj[key] = value
    },  

changeRule(myObj, 'myKey', 1)

Upvotes: 0

Views: 104

Answers (3)

lukechambers91
lukechambers91

Reputation: 711

This is what I went with. Thanks for all the help! I learned a lot from this post.

var myObj = {
      myKey: 0
    }
    var obj1 = {
      myKey1: 0
    }
    var obj2 = {
      myKey2: 0
    }
    var array = [{
      obj: obj1,
      key: 'myKey1',
      value: 1
    }, {
      obj: obj2,
      key: 'myKey2',
      value: 2
    }]
    
    function changeRule() {
            if (arguments.length == 1 && Array.isArray(arguments[0])) {
                arguments[0].forEach(e => {
                    e.obj[e.key] = e.value
                });
              } else if (arguments.length == 3 && typeof arguments[0] == "object" && typeof arguments[1] == "string" && typeof arguments[2] == "number") {
                arguments[0][arguments[1]] = arguments[2]
              } else {
                  return "Unacceptable input parameters"
              }
        }
    console.log(obj1)
    console.log("------------------------")
    changeRule(array);
    console.log(obj1)

Upvotes: 0

AbhaY
AbhaY

Reputation: 126

Check if it works for your problem statement, copy code and run in developer console, it won't run here properly.

var obj1 = {
  myKey: 0
};
var obj2 = {
  myKey: 0
};
var array = [{
  obj: 'obj1',
  key: 'myKey',
  value: 1
}, {
  obj: 'obj2',
  key: 'myKey',
  value: 2
}]

function changeRule(objOrArray, key, value) {
  if (objOrArray !== undefined && Array.isArray(objOrArray)) {
    //code that would change value of all objects according to data in array
    objOrArray.forEach(_obj => {
      eval(_obj.obj)[_obj.key] = _obj.value;
    });
    return;
  } else if (objOrArray !== undefined && key !== undefined && value !== undefined) {
    objOrArray[key] = value;
  } else {
    console.warn('param1 must be an Array, or Object with param2(key) & param3(value)');
  }
  return;
}

changeRule(array);

console.log(obj1, obj2);

Note: I have made a change in the structure of array of objects. (obj name is should be a string.)

Upvotes: 0

brk
brk

Reputation: 50291

Check if 4th parameter is undefined and if it is an array

var myObj = {
  myKey: 0
}
var obj1 = {
  myKey: 0
}
var obj2 = {
  myKey: 0
}
var array = [{
  obj: obj1,
  key: 'myKey1',
  value: 1
}, {
  obj: obj2,
  key: 'myKey2',
  value: 2
}]

function changeRule(obj, key, value, array) {
  if (array! == undefined && Array.isArray(array)) {
    // rest of the code to change the object properties
    return;
  }
  obj[key] = value
},

changeRule(myObj, 'myKey', 1, array)

Upvotes: 2

Related Questions