CoffeeScriptTips

Mixing 2 Array Objects Included Dictionary Object by Javascript

Gists This is a sample script for combining and mixing 2 objects. Each object is an array which included a dictionary type. When the key of the dictionary object is the same, the values are mixed. This can be also used for Google Apps Script. Input var obj1 = [ {"key1": ["value1a1", "value1a2"]}, {"key1": ["value1aa1", "value1aa2"]}, {"key2": ["value2a1", "value2a2"]}, {"key3": ["value3a1", "value3a2"]}, ]; var obj2 = [ {"key1": ["value1b1", "value1b2"]}, {"key3": ["value3b1", "value3b2"]}, {"key3": ["value3bb1", "value3bb2"]}, {"key4": ["value4b1", "value4b2"]}, ]; Output [ {"key1": ["value1a1", "value1a2", "value1b1", "value1b2", "value1aa1", "value1aa2"]}, {"key2": ["value2a1", "value2a2"]}, {"key3": ["value3a1", "value3a2", "value3b1", "value3b2", "value3bb1", "value3bb2"]}, {"key4": ["value4b1", "value4b2"]} ] Sample script : Javascript : function mixture(obj1, obj2) { Array.

Removing Duplicated Values in Array using CoffeeScript

Gists This sample script is for removing duplicated values in array using CoffeeScript. ar = ["a", "b", "c", "a", "c"] res = ar.filter (e, i, s) -> s.indexOf(e) is i console.log res >>> [ 'a', 'b', 'c' ] The result which was compiled by CoffeeScript is as follows. var ar, res; ar = ["a", "b", "c", "a", "c"]; res = ar.filter(function(e, i, s) { return s.indexOf(e) === i; }); console.log(res);

Flattening Nested Array using CoffeeScript

This sample flattens a nested array using CoffeeScript. flatten = (array) -> array.reduce(((x, y) -> if Array.isArray(y) then x.concat(flatten(y)) else x.concat(y)), []) console.log flatten [1, [2, 3, [4, 5]], 6, [7, [8, [9], 10] ,11 , 12], 13] >>> [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]