0
[{"First Name":"qwe","member":"complainant"},{"Last Name":"qwee","member":"scienceclub"},{"Middle Name":"wqe","member":"complainant"}]

I have only this json above how can i combine/flatten/merge them so that i can get the resulting json like

[{"First Name":"qwe","Last Name":"qwe","Middle Name":"wqe","member":"scienceclub"}]

I search for a function in jquery but all i see are the ones with 2 object.

Any suggestion is appreciated

UPDATE

I dont separate json array it is just one with arrays inside is it possible to merge the array inside of []?

guradio
  • 15,524
  • 4
  • 36
  • 57
  • 1
    And which value do you expect to pick up for `member` property? Your original array has different value for member, the second only have one with one value. What's the logic behind keeping `scienceclub` over the rest? – GillesC Jul 01 '15 at 06:44
  • 1
    Are you looking for $.extend ?http://stackoverflow.com/questions/929776/merging-associative-arrays-javascript – Balint Domokos Jul 01 '15 at 06:47
  • @gillesc they will be the same so anywhere will do – guradio Jul 01 '15 at 06:47
  • @BalintDomokos as you can see i only have i object i just want it to look like the illutrated out put the example you gave is a bit different – guradio Jul 01 '15 at 06:51

3 Answers3

0

Suppose your array like this

var arr = [{"First Name":"qwe","member":"complainant"},{"Last Name":"qwee","member":"scienceclub"},{"Middle Name":"wqe","member":"complainant"}];

use extend this way to get your job done

var obj1 = arr[0];
$.extend(obj1, arr[2]);
$.extend(obj1, arr[1]);

The result will be in obj1 as

{"Last Name":"qwee", "Middle Name":"wqe", "Last Name":"qwee", "member":"scienceclub"}

Shri
  • 834
  • 1
  • 12
  • 36
0

var a = [{
  "First Name": "qwe",
  "member": "complainant"
}, {
  "Last Name": "qwee",
  "member": "scienceclub"
}, {
  "Middle Name": "wqe",
  "member": "complainant"
}];
alert(JSON.stringify(
  $.extend.apply(null, a)
  , null, '\t'
));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Vladimir
  • 342
  • 1
  • 8
0

You can try this:

arr = [{"First Name":"qwe","member":"complainant"},{"Last     Name":"qwee","member":"scienceclub"},{"Middle Name":"wqe","member":"complainant"}]
arr.splice(0, 0, {})
result = $.extend.apply(this, arr)

You still need to deal with multiple keys, this will use the last one. The splice is needed to avoid changing the first object of the array.

Balint Domokos
  • 1,021
  • 8
  • 12