-1

This is my code:

var data = {};
data.uuid = uuid;
data.method = "addAddress";

//Other parameters
var par = {};
par.name = "test";
par.surname = "test";

data.concat(par); //doesn't work

var url = myURL;
var result = $.ajax({
    url: url,
    data: data,
    async: false
});

I want to merge two variables array. It is possible to merge this variables?

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
facecard
  • 73
  • 6

2 Answers2

0

Variables data and par are objects and there is not concat method in Objects.. So you end up with a TypeError: "data.concat is not a function".

But you can merge the two Objects like this:

var data = {},
    par = {};

data.uuid = 1234;
data.method = 'addAddress';

par.name = 'test';
par.surname = 'test';

Object.keys(par).forEach(k => data[k] = par[k]);

console.log(data);

Note that properties of data that are also in par will be overwritten.

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

Check this code.

I have changed your var.surname = "test" code into par.surname = "test"

var data = {};
data.uuid = 1;
data.method = "addAddress";

var par = {};
par.name = "test";
par.surname = "test";

var obj = Object.assign({}, data, par);

console.log(obj);
UWU_SANDUN
  • 1,123
  • 13
  • 19