- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2021 04:54 AM
var a = {
"a": "1",
"b": "2"
};
var b = {
"c": "1",
"d": "2"
};
var c = Object.assign(a, b);
gs.log(c);
why ;)?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2021 05:17 AM
Try below
var a = {
"a": "1",
"b": "2"
};
var b = {
"c": "1",
"d": "2"
};
gs.print(JSON.stringify(merge(a,b)))
function merge(from,to){
for (var name in from){
var mached=false;
for(var dt in to){
if(dt==name)
mached=true;
}
if(!mached)
to[name]=from[name];
}
return to;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2021 04:59 AM
Hi,
you can use this sample script to merge
var a = {
"a": "1",
"b": "2"
};
var b = {
"c": "1",
"d": "2"
};
var arr = [];
arr.push(a);
arr.push(b);
gs.info(JSON.stringify(arr));
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2021 11:23 PM
Do you require to merge those 2 objects?
then using array would be easier.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2021 05:17 AM
Try below
var a = {
"a": "1",
"b": "2"
};
var b = {
"c": "1",
"d": "2"
};
gs.print(JSON.stringify(merge(a,b)))
function merge(from,to){
for (var name in from){
var mached=false;
for(var dt in to){
if(dt==name)
mached=true;
}
if(!mached)
to[name]=from[name];
}
return to;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2022 06:37 AM
I believe the answers are just trying to circumvent a BUG!
The point is Object.assign should work on ServiceNow since it is a standard javascript prototype function.