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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM