lodash merge mergeWith使用

一、做用javascript

遞歸合併來源對象的自身和繼承的可枚舉屬性到目標對象。 css

二、示例html

<!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>
相關文章
相關標籤/搜索