1
I have the following dilemma, I need to assemble 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 don’t know which one is right, or if none of them are 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 think I write too much, if you notice, I did this because some of the object values, I’m going to take either from one variable already declared, or from another. (in this object I put only 7 items to not get too extensive, but in my case there are more than 20 items, so my code gets even bigger by typing the object twice.
After that I thought of making 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, of a total of approximately 23. so doing this way, I managed to save some lines.
But I wasn’t happy yet, I know the push function and the Concat function (in my project I use jquery and angular.js tbm).
But with these functions, it forms 1 array with 2 objects. There is a way to do this "merge" that results in 1 array of 1 single object?
You want to change the values of an "Objetoarray" based on a condition, that’s it?
– Guilherme Lautert