SHARE
TWEET

Untitled

a guest May 26th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const _ = require('lodash');
  2.  
  3. console.log(_.isObject({})); // true
  4. console.log(_.isObject([])); // true
  5. console.log(_.isObject(1)); // false
  6.  
  7. const a = {
  8.   a: 'stringA',
  9.   b: 123,
  10.   c: [1, 2, 3, 7, 8],
  11.   d: {
  12.     e: 123,
  13.     g: {
  14.       z: 123
  15.     },
  16.     x: 999
  17.   },
  18.   y: 555
  19. };
  20.  
  21. const b = {
  22.   a: 'stringB',
  23.   b: 321,
  24.   c: [4, 5, 6],
  25.   d: {
  26.     e: 321,
  27.     f: 555
  28.   }
  29. };
  30.  
  31. console.log(_.merge(a, b));
  32. /*
  33. { a: 'stringB',
  34.   b: 321,
  35.   c: [ 4, 5, 6, 7, 8 ],
  36.   d: { e: 321, f: 555 } }
  37. { a: [ { b: 2, c: 3 }, { d: 4, e: 5 } ] }
  38. */
  39.  
  40. // 配列はoverwriteするように修正
  41. const customizer = (a, b) => {
  42.   if (_.isArray(a)) {
  43.     return b;
  44.   }
  45. }
  46. console.log(_.mergeWith(a, b, customizer));
  47. /*
  48. { a: 'stringB',
  49.   b: 321,
  50.   c: [ 4, 5, 6 ],
  51.   d: { e: 321, g: { z: 123 }, x: 999, f: 555 },
  52.   y: 555 }
  53. */
RAW Paste Data
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