4

I need to join two json object using angularJs.

My Object example:

 {
  "Details1" : [ {
    "Details1Id" : 119,
    "Details1title" : "Title1",
    "Details1description" : "Description1"

  } ],
  "Details2" : [ {
    "Details2Id" : 150,
    "Details2title" : "Title2",
    "Details2description" : "Description2"

  } ]
}

I have objcet like this I need to join Details2 with Details1

I need the result in following manner. Is it possible with angularJS ? Please suggest

{
  "Details1" : [ {
    "Details1Id" : 119,
    "Details1title" : "Title1",
    "Details1description" : "Description1"

  } ,{

  "Details2Id" : 150,
    "Details2title" : "Title2",
    "Details2description" : "Description2"
  }]
}
2
  • 3
    how do you know that details2 should be merde with detail1 ? Commented Jun 19, 2015 at 12:25
  • 5
    Yes, it's possible with JavaScript. But we need to know how the code will be able to determine what gets combined with what. I assume the data you are combining isn't exactly what you posted. Commented Jun 19, 2015 at 12:28

2 Answers 2

1

You may try this,

$scope.jsonArray['Details1'].push($scope.jsonArray['Details2'][0])

where $scope.jsonArray is the variable storing the json, you may use any other variable.

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

Comments

0

Depending on which version of AngularJs you are using, you can use angular.extend or angular.merge (since version 1.4 I believe)

Angular.extend can not merge nested objects, the later declaration will win. Angular.merge, as the name suggests, merge 2 objects together without losing data.

See the docs Extend - https://docs.angularjs.org/api/ng/function/angular.extend

Merge - https://docs.angularjs.org/api/ng/function/angular.merge

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.