deep gagan
deep gagan

Reputation: 50

How to assign JSON object

I'm New in AngularJS. i'm trying to assign JSON objects and i'm using Object.assign to assign JSON objects. But I can't get the result that i want Please help me to get the result.

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

Upvotes: 0

Views: 145

Answers (2)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27202

Try this :

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

Upvotes: 1

vusan
vusan

Reputation: 5331

You can extend objects with $.extend(a, b); using jQuery but as you are using angularjs, you can use core 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)

Upvotes: 0

Related Questions