Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const _ = require('lodash');
- console.log(_.isObject({})); // true
- console.log(_.isObject([])); // true
- console.log(_.isObject(1)); // false
- const a = {
- a: 'stringA',
- b: 123,
- c: [1, 2, 3, 7, 8],
- d: {
- e: 123,
- g: {
- z: 123
- },
- x: 999
- },
- y: 555
- };
- const b = {
- a: 'stringB',
- b: 321,
- c: [4, 5, 6],
- d: {
- e: 321,
- f: 555
- }
- };
- console.log(_.merge(a, b));
- /*
- { a: 'stringB',
- b: 321,
- c: [ 4, 5, 6, 7, 8 ],
- d: { e: 321, f: 555 } }
- { a: [ { b: 2, c: 3 }, { d: 4, e: 5 } ] }
- */
- // 配列はoverwriteするように修正
- const customizer = (a, b) => {
- if (_.isArray(a)) {
- return b;
- }
- }
- console.log(_.mergeWith(a, b, customizer));
- /*
- { a: 'stringB',
- b: 321,
- c: [ 4, 5, 6 ],
- d: { e: 321, g: { z: 123 }, x: 999, f: 555 },
- y: 555 }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement