如何分配JSON对象

时间:2017-04-06 11:12:57

标签: javascript json

我是AngularJS的新手。我正在尝试分配JSON对象,我正在使用Object.assign来分配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	=	{"Settings":{"Basic Settings":{"My Information":"1","Bank Information":"2"}}};

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

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

2 个答案:

答案 0 :(得分:1)

试试这个:

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);

答案 1 :(得分:0)

您可以使用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)