如何分配JSON对象

问题描述:

我是AngularJS中的新成员。我试图分配JSON对象,我使用Object.assign来分配JSON对象。但是我不能得到我想要的结果 请帮我拿到结果。如何分配JSON对象

'use strict' 
 

 
let one ={"Start":"11","Dashboard":"1","Settings":"2","Support":"3","Trunk":"4","Routing":"5","Solutions":"6","Accounting":"7","Statistic":"8","Marketing":"9","Profile":"10"}; 
 
let two = {"Settings":{"Basic Settings":"1","Number Range":"2","Employess":"3","Message Fulfillment":"4"},"Support":{"All Contacts":"5","Detail Approval":"6","Incomplete Signup":"7","Settings":"8"},"Trunk":{"My Tickets":"9","All Tickets":"10"}}; 
 

 
let three \t = \t {"Settings":{"Basic Settings":{"My Information":"1","Bank Information":"2"}}}; 
 

 
let four = Object.assign({}, one, two, three); 
 

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

+0

我需要这样的: - { “开始”: “11”, “仪表板”: “1”, “设置”:{ “基本设置”:{ “我的信息”: “1”,”银行信息“:”2“},”号码范围“:”2“,”职业“:”3“,”信息实现“:”4“},”支持“:{”所有联系人“:”5“, “详细审批”:“6”,“未完成注册”:“7”,“设置”:“8”},“中继线”:{“我的门票”:“9”,“所有门票”:“10”} , “路由”: “5”, “解决方案”: “6”, “会计”: “7”, “统计”: “8”, “市场”: “9”, “个人资料”: “10”,”基本设置“:{”我的信息“:”1“,”银行信息“:”2“}} –

+0

你有答案吗? –

+0

@deepgagan你检查了我的答案吗? –

您可以使用jQuery $.extend(a, b);扩展对象,但因为使用的是angularjs,您可以使用JavaScript核心

let one ={"Start":"11","Dashboard":"1","Settings":"2","Support":"3","Trunk":"4","Routing":"5","Solutions":"6","Accounting":"7","Statistic":"8","Marketing":"9","Profile":"10"}; 
let two = {"Settings":{"Basic Settings":"1","Number Range":"2","Employess":"3","Message Fulfillment":"4"},"Support":{"All Contacts":"5","Detail Approval":"6","Incomplete Signup":"7","Settings":"8"},"Trunk":{"My Tickets":"9","All Tickets":"10"}}; 

let three = {"Settings":{"Basic Settings":{"My Information":"1","Bank Information":"2"}}}; 


function jsonExtend(a, b) { 
for (var key in b) { 
    a[key] = b[key]; 
} 
return a; 
} 

var output = {}; 
output = jsonExtend(output, one); 
output = jsonExtend(output, two); 
output = jsonExtend(output, three); 

console.log(output) 
+0

输出不显示: - “Number Range”:“2”,“Employess”:“3”,“Message Fulfillment”:“4” –

+0

当连接对象时,属性被重复,因此第三个覆盖第二个。 – vusan

+0

好的。但我需要这些值。那么我怎么能得到这些值? –

试试这个:

function mergeObjects() { 
 
    var merged_obj = {}; 
 
    for (i in arguments) { 
 
     obj = arguments[i]; 
 
     for (j in obj) { 
 
       merged_obj[j] = obj[j]; 
 
     } 
 
    } 
 
    return merged_obj; 
 
} 
 

 
var one ={"Start":"11","Dashboard":"1","Settings":"2","Support":"3","Trunk":"4","Routing":"5","Solutions":"6","Accounting":"7","Statistic":"8","Marketing":"9","Profile":"10"}; 
 
var two = {"Settings":{"Basic Settings":"1","Number Range":"2","Employess":"3","Message Fulfillment":"4"},"Support":{"All Contacts":"5","Detail Approval":"6","Incomplete Signup":"7","Settings":"8"},"Trunk":{"My Tickets":"9","All Tickets":"10"}}; 
 

 
var three = {"Settings":{"Basic Settings":{"My Information":"1","Bank Information":"2"}}}; 
 

 
var mergedObj = mergeObjects(one, two, three); 
 
console.log(mergedObj);

+0

它不显示这些值: - “编号范围”:“2”,“职业”:“3”,“消息实现”:“4” –