美文网首页
js数组对象中的操作

js数组对象中的操作

作者: 糖醋里脊120625 | 来源:发表于2019-08-09 17:32 被阅读0次
    
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>vue自定义过滤器</title>
            <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        </head>
        <body>
            <div class="test">
     
            </div>
            <script type="text/javascript">
                //对象操作
                let meals = {
                    mealA: 'Breakfast',
                    mealB: 'Lunch',
                    mealC: 'Dinner'
                };
                for(let key of Object.keys(meals)) {
                    let mealName = meals[key];
                    // ... do something with mealName
                    console.log(mealName);
                }
                
                
                //对象操作
                let simpleColors = {
                    colorA: 'white',
                    colorB: 'black'
                };
                let natureColors = {
                    colorC: 'green',
                    colorD: 'yellow'
                };
                Object.setPrototypeOf(natureColors, simpleColors);
                Object.keys(natureColors); // => ['colorC', 'colorD']
                natureColors['colorA'];    // => 'white'
                natureColors['colorB'];    // => 'black'
                
                
                //函数式
                // 简单的缓存工具
                // 匿名函数创造了一个闭包
                const cache = (function() {
                    const store = {};
                    return {
                        get(key) {
                            return store[key];
                        },
                        set(key, val) {
                          store[key] = val;
                        }
                    }
                }());
                console.log(cache) //{get: ƒ, set: ƒ}
                cache.set('a', 1);
                cache.get('a');  // 1
                
                
            //不是高阶函数
            ////map()
            //map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。map 不会改变原数组
            var animals = [
                { name: "Fluffykins", species: "rabbit" },
                { name: "Caro", species: "dog" },
                { name: "Hamilton", species: "dog" },
                { name: "Harold", species: "fish" },
                { name: "Ursula", species: "cat" },
                { name: "Jimmy", species: "fish" }
            ];
            var names = [];
                for (let i = 0; i < animals.length; i++) {
                names.push(animals[i].name);
            }
            console.log(names); //["Fluffykins", "Caro", "Hamilton", "Harold", "Ursula", "Jimmy"]
            //高阶函数
            var animals = [
                { name: "Fluffykins", species: "rabbit" },
                { name: "Caro", species: "dog" },
                { name: "Hamilton", species: "dog" },
                { name: "Harold", species: "fish" },
                { name: "Ursula", species: "cat" },
                { name: "Jimmy", species: "fish" }
            ];
            var names = animals.map(x=>x.name);
            console.log(names); //["Fluffykins", "Caro", "Hamilton", "Harold", "Ursula", "Jimmy"]
            
            
            // 不使用高阶函数 
            //filter() 方法会创建一个新数组,其中包含所有通过回调函数测试的元素。
            //filter 为数组中的每个元素调用一次 callback 函数, callback 函数返回 true 表示该元素通过测试,保留该元素,false 则不保留。filter 不会改变原数组,它返回过滤后的新数组。
            var animals = [
                { name: "Fluffykins", species: "rabbit" },
                { name: "Caro", species: "dog" },
                { name: "Hamilton", species: "dog" },
                { name: "Harold", species: "fish" },
                { name: "Ursula", species: "cat" },
                { name: "Jimmy", species: "fish" }
            ];
            var dogs = [];
            for (var i = 0; i < animals.length; i++) {
                if (animals[i].species === "dog") dogs.push(animals[i]);
            }
            console.log(dogs); 
            //使用高阶函数filter
            var animals = [
                { name: "Fluffykins", species: "rabbit" },
                { name: "Caro", species: "dog" },
                { name: "Hamilton", species: "dog" },
                { name: "Harold", species: "fish" },
                { name: "Ursula", species: "cat" },
                { name: "Jimmy", species: "fish" }
            ];
            var dogs = animals.filter(x => x.species === "dog");
            console.log(dogs); // {name: "Caro", species: "dog"}
            
            
            // 不使用高阶函数
            //reduce
            //reduce 方法对调用数组的每个元素执行回调函数,最后生成一个单一的值并返回。 reduce 方法接受两个参数:1)reducer 函数(回调),2)一个可选的 initialValue。
            const arr = [5, 7, 1, 8, 4];
            let sum = 0;
            for (let i = 0; i < arr.length; i++) {
              sum = sum + arr[i];
            }
            console.log(sum);//25
            // 使用高阶函数
            const arr = [5, 7, 1, 8, 4];
            const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue,0);
            console.log(sum)//25
            </script>
        </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:js数组对象中的操作

          本文链接:https://www.haomeiwen.com/subject/jsahjctx.html