• 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"]
    
    
    
     
  • 相关阅读:
    BZOJ5104 二次剩余板子
    BZOJ5329 [Sdoi2018]战略游戏 圆方树+虚树
    BZOJ1095 动态点分治
    BZOJ3992: [SDOI2015]序列统计
    kd-tree板子
    thusc2018翻车记
    BZOJ5336 DP套DP
    BZOJ4316 仙人掌DP
    问题 F: 最小花费
    问题 C: 热浪
  • 原文地址:https://www.cnblogs.com/tsai-87/p/12162225.html
Copyright © 2020-2023  润新知