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 []?

4
  • 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? Commented Jul 1, 2015 at 6:44
  • 1
    Are you looking for $.extend ?stackoverflow.com/questions/929776/… Commented Jul 1, 2015 at 6:47
  • @gillesc they will be the same so anywhere will do Commented Jul 1, 2015 at 6: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 Commented Jul 1, 2015 at 6:51

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

Comments

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"}

2 Comments

I have only one object i dont have 3 this is result of getting all input text from a table tr so i cant really separate them.
Check it out! might be helpful
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.