Collect.js union() Method Last Updated : 14 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The union() method is used to add the given array to the collection with the unique values. If the given array contains values that are already in the original collection, the original collection's values will be preferred. Syntax: collect.union() Parameters: The collect() method takes one argument that is converted into the collection and then union() method is applied on it. Return Value: This method returns the collection with the unique elements. Below example illustrate the union() method in collect.js: Example 1: javascript const collect = require('collect.js'); let obj = [1, 2, 3, 4, 5]; const collection = collect(obj); const union = collection.union([1, 2, 3, 2, 4]); console.log(union.all()); Output: [1, 2, 3, 4, 5] Example 2: javascript const collect = require('collect.js'); let obj = ({ name1: 'AAA', name2: 'BBB', }); const collection = collect(obj); const union = collection.union({ name1: 'AAAAA', name3: 'CCCCC', name2: 'BBBBB', }); console.log(union.all()); Output: {name1: "AAA", name2: "BBB", name3: "CCCCC"} Comment More infoAdvertise with us Next Article Collect.js pop() Method C code_hunt Follow Improve Article Tags : JavaScript Web Technologies Collect.js Similar Reads Collect.js only() Method The only() method is used to return the items from the given collection with the specified keys. It takes the key as a parameter and returns the item mapped with that key. Syntax: collect(array).only(key)Parameters: The collect() method takes one argument that is converted into the collection and th 2 min read Collect.js unique() Method The unique() method is used to return the all of the unique values in the collection. Syntax: collect(array).unique() Parameters: The collect() method takes one argument that is converted into the collection and then unique() method is applied on it. Return Value: This method returns all of the uniq 1 min read Collect.js sum() Method The sum() method in collect.js is used to return the sum of all the items in the given collection. Syntax: collect(array).sum() Parameters: The collect() method takes one argument that is converted into the collection and then sum() method is applied on it. Return Value: This method returns the sum 1 min read Collect.js put() Method The put() method is used to set the given key and value in the collection. Syntax: collect(array).put(key) Parameters: The collect() method takes one argument that is converted into the collection and then put() method is applied on it. The put() method holds the key as parameter. Below example illu 1 min read Collect.js pop() Method The pop() method is used to remove the last element from collection and returns the popped element. Syntax: collect(array).pop() Parameters: The collect() method takes one argument that is converted into the collection and then pop() method is applied on it. Return Value: This method returns the pop 1 min read Collect.js unwrap() Method The unwrap() method is used to unwrap the given collection. Syntax: collect(array).unwrap() Parameters: The collect() method takes one argument that is converted into the collection and then unwrap() method is applied on it. Return Value: This method returns a array or object. Below example illustra 1 min read Like