• JavaScript Experiment Collection


    
    
    function getKeysOfObject() {
        let o = {'a': 1, 'b': function() {}, 'c': 2}; 
        console.log(Object.keys(o));
    }
    getKeysOfObject();
    Result:["a", "b", "c"]
    ----------------------------------------------------------------------------------------------------------------------------
    function addPropertyToObj() {
        let o = {};
        o['a'] = 87;
        console.log(o);
    }
    addPropertyToObj();
    Result: {a: 87}
    ----------------------------------------------------------------------------------------------------------------------------
    function processStringWithMap() {
        let str = 'hello';
        let map = Array.prototype.map;
        let a = map.call(str, function(e) { return e.concat('N')});
        console.log(a);
    }
    processStringWithMap();
    Result: ["hN", "eN", "lN", "lN", "oN"]
    ----------------------------------------------------------------------------------------------------------------------------
    function calculateSumOfNumberArray() {
       let total = [1,2,3].reduce((accumulator, currentValue)=> {return accumulator + currentValue});
       console.log(total);
    }
    calculateSumOfNumberArray();
    Result: 6
    ----------------------------------------------------------------------------------------------------------------------------
    function calculateSumOfObjectArray() {
       let total = [{x:1},{x:2},{x:3}].reduce((accumulator, currentValue) => {return accumulator + currentValue.x},0);
       console.log(total);
    }
    calculateSumOfObjectArray();
    Result: 6
    ----------------------------------------------------------------------------------------------------------------------------
    function flattenArray() {
      let newArray = [[0,1],[2,3],[4,5]].reduce((accumulator, currentValue)=> {return accumulator.concat(currentValue)},[]);
      console.log(newArray);
    }
    flattenArray();
    Result: [0,1,2,3,4,5]
    ----------------------------------------------------------------------------------------------------------------------------
    function countInstancesOfValuesOfArrayInObject() {
      let names = ['Alice', 'Bob', 'Tiff', 'Bob', 'Alice'];
      let countedNames = names.reduce((allNames, name)=> { 
          if(name in allNames) {
            allNames[name]++;
          } else {
            allNames[name] = 1;
          }
          return allNames;
      },{})
      console.log(countedNames);
    }
    countInstancesOfValuesOfArrayInObject();
    Result: {Alice: 2, Bob: 2, Tiff: 1}
    ----------------------------------------------------------------------------------------------------------------------------
    function groupBy(objectArray, property) {
      return objectArray.reduce(function (acc, obj) {
        let key = obj[property]
        if (!acc[key]) {
          acc[key] = []
        }
        acc[key].push(obj)
        return acc
      }, {})
    }
    
    function applyGroup() {
        let people = [
          { name: 'Alice', age: 21 },
          { name: 'Max', age: 20 },
          { name: 'Jane', age: 20 }
        ];
        let groupedPeople = groupBy(people, 'age');
        console.log(groupedPeople);
    }
    applyGroup();
    Result:
    { 
        20: [
         { name: 'Max', age: 20 }, 
         { name: 'Jane', age: 20 }
        ], 
        21: [{ name: 'Alice', age: 21 }] 
    }
    ----------------------------------------------------------------------------------------------------------------------------
    function removeDuplicatedItem() {
      let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd'];
      let myOrderedArray  = myArray.reduce((accumulator, currentValue)=>{
        if (accumulator.indexOf(currentValue) === -1) {
            accumulator.push(currentValue)
        }
        return accumulator
      },[]);
      return myOrderedArray;
    }
    removeDuplicatedItem();
    Result: ["a", "b", "c", "e", "d"]
    
    
    
     
  • 相关阅读:
    五分钟快速搭建Serverless免费邮件服务
    【Python基础编程240 ● 异常 ● 异常的跨函数传递】
    【Python基础编程239 ● 异常 ● 异常语句中else语句的使用】
    【Python基础编程238 ● 异常 ● 异常语句中else语句的使用】
    【Python基础编程237 ● 异常 ● 异常处理的基本格式:单独捕获单独处理】
    【Python基础编程236 ● 异常 ● 异常处理的基本格式】
    【Python基础编程235 ● 异常 ● 异常处理的基本格式】
    【Python基础编程234 ● 异常 ● 异常的概述】
    【Python基础编程233 ● 面向对象 ● 多态】
    【Python基础编程232 ● 面向对象 ● 静态方法】
  • 原文地址:https://www.cnblogs.com/tsai-87/p/12162225.html
Copyright © 2020-2023  润新知