打赏

lodash merge mergeWith使用

1、作用

递归合并来源对象的自身和继承的可枚举属性到目标对象。 

2、示例

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>lodash mergeWith使用</title>
    </head>

    <body>
        <script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script>
        <script type="text/javascript">
            function customizer(objValue, srcValue) {
                if(_.isArray(objValue)) {
                    return objValue.concat(srcValue);
                }
            }

            var object = {
                'fruits': ['apple'],
                'vegetables': ['beet']
            };

            var other = {
                'fruits': ['banana'],
                'vegetables': ['carrot']
            };

            let o1 = _.mergeWith(object, other, customizer);
            let o2 = _.mergeWith(object, other);
            let o3 = _.merge(object, other);
            console.log(_.isEqual(o1, o2)) //true
            console.log(_.isEqual(o3, o2)) //true
        </script>
    </body>

</html>

 

posted @ 2018-09-04 15:25  孟繁贵  阅读(4103)  评论(0编辑  收藏  举报
TOP