G2A Many GEOs
SHARE
TWEET

flatten-array

Rafael_Minguet Mar 31st, 2020 (edited) 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import test from 'tape';
  2.  
  3. const flattenLoop = (acc, val) => !Array.isArray(val) ? [...acc, val] : val.reduce(flattenLoop, acc);
  4. const flatten = array => flattenLoop([], array);
  5.  
  6. // TESTS ------------------------------------------------------------
  7.  
  8. test(`-------- Flatten: testing conversion from multidimensional array to one dimension array`, (assert) => {
  9.     const initialArray = [1, [3, true, [5, 3]], 'aa', [3, 'cc']];
  10.     const expected     = [1, 3, true, 5, 3, 'aa', 3, 'cc'];
  11.     const message      = `${JSON.stringify(initialArray)} should be converted to ${JSON.stringify(expected)}`;
  12.  
  13.     const actual = flatten(initialArray);
  14.  
  15.     assert.deepEqual(actual, expected, message);
  16.     assert.end();
  17. });
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top