0

I have two objects

var data = {property: [
  {id: "1", name: "Snatch", type: "crime"},
  {id: "2", name: "Witches of Eastwick", type: "comedy"},
  {id: "3", name: "X-Men", type: "action"},
  {id: "4", name: "Ordinary People", type: "drama"},
  {id: "5", name: "Billy Elliot", type: "drama"},
  {id: "6", name: "Toy Story", type: "children"}
]};

and

var data = {property: [
  {id: "7", name: "Snatch", type: "crime"},
  {id: "8", name: "Witches of Eastwick", type: "comedy"}
]};

Can I use jquery or javascript to combine the two as follows

var data = {property: [
  {id: "1", name: "Snatch", type: "crime"},
  {id: "2", name: "Witches of Eastwick", type: "comedy"},
  {id: "3", name: "X-Men", type: "action"},
  {id: "4", name: "Ordinary People", type: "drama"},
  {id: "5", name: "Billy Elliot", type: "drama"},
  {id: "6", name: "Toy Story", type: "children"},
  {id: "7", name: "Snatch", type: "crime"},
  {id: "8", name: "Witches of Eastwick", type: "comedy"}
]};

I've tried to use the push function but with no luck

2
  • 3
    You have no JSON there at all. You have JavaScript objects. Commented Jun 5, 2014 at 15:58
  • … and it is the arrays you are trying to combine. Commented Jun 5, 2014 at 16:00

2 Answers 2

2

How about:

var data = {property: []}

var data2 = {property: []}

data.property =data.property.concat(data2.property);

Using concat function of the Array you can join them.

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

Comments

0

concat is one solution, but underscore.js' union will take care of any duplicates as well.

var data3 = _.union(data.property, data2.property)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.