2016-01-21 72 views
-3

我有一個JSON作爲下合併兩個JSON在Javascript或JQuery的

"chart": { 
     "xAxisname": "Months", 
     "yAxisName": "Year" 
    } 

第二的Json我從的WebAPI接收作爲下

[ 
    { 
     "productName": "Personal Loan", 
     "loanAmount": [ 
      { 
       "amount": 121000 
      },    
      { 
       "amount": 145000 
      } 
     ] 
    }, 
    { 
     "productName": "Car Loan", 
     "loanAmount": [ 
      { 
       "amount": 180000 
      }, 
      { 
       "amount": 210000 
      } 
     ] 
    } 
] 

我要尋找的輸出

{ 
    "chart": { 
     "xAxisname": "Months", 
     "yAxisName": "Year" 
    },  
    "dataset": [ 
     { 
      "productName": "Personal Loan", 
      "loanAmount": [ 
       { 
        "amount": "121000" 
       },    
       { 
        "amount": "145000" 
       } 
      ] 
     }, 
     { 
      "productName": "Car Loan", 
      "loanAmount": [ 
       { 
        "amount": "180000" 
       }, 
       { 
        "amount": "210000" 
       } 
      ] 
     } 
    ] 
} 

我試過$ .extend作爲根據

var obj1 = { 
        "xAxisname": "Months", 
        "yAxisName": "Year" 
        }; 

     var obj2 = [ 
         { 
          "productName": "Personal Loan", 
          "loanAmount": [ 
           { 
            "amount": 121000 
           },    
           { 
            "amount": 145000 
           } 
          ] 
         }, 
         { 
          "productName": "Car Loan", 
          "loanAmount": [ 
           { 
            "amount": 180000 
           }, 
           { 
            "amount": 210000 
           } 
          ] 
         } 
       ]; 

     var finalObj = $.extend(obj1, obj2); 

儘管它正在合併,但並不像預期的那樣。

請幫

+2

'VAR finalobj = { '圖':OBJ1, '數據集':OBJ 2}'? –

回答

1

var obj1 = { 
 
    "xAxisname": "Months", 
 
    "yAxisName": "Year" 
 
}; 
 

 
var obj2 = [{ 
 
    "productName": "Personal Loan", 
 
    "loanAmount": [{ 
 
    "amount": 121000 
 
    }, { 
 
    "amount": 145000 
 
    }] 
 
}, { 
 
    "productName": "Car Loan", 
 
    "loanAmount": [{ 
 
    "amount": 180000 
 
    }, { 
 
    "amount": 210000 
 
    }] 
 
}]; 
 

 
var finalObj = $.extend({chart: obj1}, {dataset: obj2}); 
 

 
// or just 
 

 
var otherObj = { 
 
    chart: obj1, 
 
    dataset: obj2 
 
} 
 

 
console.log(finalObj); 
 
console.log(otherObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

1

var j1 = 
 
    { 
 
     "xAxisname": "Months", 
 
     "yAxisName": "Year" 
 
    }; 
 

 

 
var j2 = [ 
 
    { 
 
     "productName": "Personal Loan", 
 
     "loanAmount": [ 
 
      { 
 
       "amount": 121000 
 
      },    
 
      { 
 
       "amount": 145000 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "productName": "Car Loan", 
 
     "loanAmount": [ 
 
      { 
 
       "amount": 180000 
 
      }, 
 
      { 
 
       "amount": 210000 
 
      } 
 
     ] 
 
    } 
 
]; 
 

 
var j3 = {}; 
 
j3["chart"] = j1; 
 
j3["dataset"] = j2; 
 

 
console.log(JSON.stringify(j3));