Join two objects in one with conditional attributes in JavaScript

1

I have the following dilemma, I need to mount an object to send to a API rest . But I have data that starts from a variable. I thought of several ways to do this but I do not know which one is right, or if none of them is right.

if (true){
  var obj = {
    "a": a,
    "b": b,
    "c": c,
    "d": d,
    "e": E,
    "f": F,
    "g": G,
  }
} else {
  var obj = {
    "a": a,
    "b": b,
    "c": c,
    "d": d,
    "e": H,
    "f": I,
    "g": J,
  }
}

// envio via ajax este obj para a API.

This was the first way I did it. But I thought I write too much, if you notice, I did this because some of the values of the object, I will get either a variable already declared, or another. (in this case I put only 7 items to not get too long, but in my case there are more than 20 items, so my code gets even bigger by typing the object twice.

After this I thought of doing the following way:

if (true){
 var x = E;
 var y = F;
 var z = G;
} else {
 var x = H;
 var y = I;
 var z = J;
}

var obj = {
  "a": a,
  "b": b,
  "c": c,
  "d": d,
  "e": x,
  "f": y,
  "g": z,
}

// envio via ajax este obj para a API.

In my code, the items that are conditional, are 5, out of a total of about 23. So by doing this, I managed to save a few lines.

But I was not happy yet, I know the push function and concat function (in my project I use jquery and angular.js tbm).

But with these functions, it forms 1 array with 2 objects. Is there a way to do this "merge" that results in 1 array of 1 single object?

    
asked by anonymous 06.02.2017 / 20:45

1 answer

1

Another way to do what you need is the one below. The interesting points are:

  • To store the return of the condition on a variable to, in case the condition changes over time, simply change a line of code;
  • Use the ternary operator on attributes that have conditional value, preventing it from rewriting the object, or part of it, more than once;
  • var condicao = true;
    
    var obj = {
      "a": 1,
      "b": 2,
      "c": 3,
      "d": 4,
      "e": (condicao) ? 5 : -5,
      "f": (condicao) ? 6 : -6,
      "g": (condicao) ? 7 : -7
    };
    
    console.log(obj);

    The output of the code, for the true condition:

    {
      "a": 1,
      "b": 2,
      "c": 3,
      "d": 4,
      "e": 5,
      "f": 6,
      "g": 7
    }
    

    Already, for the false condition:

    {
      "a": 1,
      "b": 2,
      "c": 3,
      "d": 4,
      "e": -5,
      "f": -6,
      "g": -7
    }
    
      

    Note : I used numerical values to reproduce the result without having to define many variables, but in its application, attribute values can be defined from variables of course. >

    Ternary operator

    The ternary operator has the following syntax:

    condition ? expr1 : expr2 
    

    Where condition should be an expression, or boolean variable, that evaluates to true or false and expr1 , expr2 are expressions with values of any type. The description of the operation is as follows: if condition is true , the operator returns the value of expr1 , otherwise, returns the value of expr2 in>.

    In this way, having a var x = a > 2? 1 : -1 code would be the same as having:

    if (a > 2) {
        x = 1;
    } else {
        x = -1;
    }
    

    Only with much less code and more readable.

        
    06.02.2017 / 21:21