how can i merge two objects into one object ?
# suitescript
k
how can i merge two objects into one object ?
b
the very netsuite answer is util.extend
s
I have more faith in
Object.assign()
,
_.assign()
or
_.merge()
than anything netsuite provides as a custom library.
b
I like lodash's assign too, though you can probably implement it fairly easily as a for loop if you want to avoid it
e
util.extend()
In case you're not using SS2.1 and don't want to pull in a library
b
netsuite's util.extend is fairly sane, i would trust it
Copy code
/**
     * @memberof util
     * @name util.extend
     *
     * @param {Object} receiver
     * @param {Object} contributor
     * @returns {Object} receiver
     */
    function extend(receiver, contributor)
    {
	    for (var key in contributor)
		    if (contributor.hasOwnProperty(key))
			    receiver[key] = contributor[key];
	    return receiver;
    }
s
I have used
util.extend
in quite a lot of projects, and it has worked reliably for me so far
s
no votes for using the native
Object.assign()
? I guess I'm tainted by watching NS's code break over the years.
b
its more that i wouldnt tell anyone to be the first to switch to 2.1
s
it's a split decision for me - I suspect the standard javascript behavior to be a big improvement in 2.1 but some of the (NS) APIs seem to be wonky in 2.1
k
Thank you everyone! Didn’t expect so many responses. I would prefer to use Object.assign as well, but dont trust 2.1 completely right now. I was able to successfully accomplish it by using util.extend though. Thanks again everyone! 🙏🏽