0

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)
4
  • 1
    You can just add a condition in your function that checks for the value of the fourth parameter and if it's not undefined just ignore the others. Commented Nov 27, 2018 at 15:38
  • 1
    How about you reuse the first parameter and check the type? Commented Nov 27, 2018 at 15:38
  • add it to the signature and test if it's defined Commented Nov 27, 2018 at 15:38
  • Franks solution seems to the the best for me. Commented Nov 27, 2018 at 15:55

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

If I do what you mentioned then changeRule(array) would not work right? I have to have the other parameters right?
Should just have two functions? One which accepts and array and another which accepts the parameters.
You can still do that by tweaking the function and checking argument length. But in that case what about if the user does not pass an array but call the function with only myObj?
0

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.)

2 Comments

Why pass the object name as a string that you have to eval() later, when you can just pass the object itself?
@lukechambers91 you are right, it seems unnecessary, my bad. BTW thanks for pointing out.
0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.