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?