zoukankan      html  css  js  c++  java
  • 一些精妙的工具函数

    一、将对象数组转换为对象

      const calendarTypeOptions = [
          { key: 'CN', display_name: '中国' },
          { key: 'US', display_name: '美国' },
          { key: 'JP', display_name: '日本' },
          { key: 'EU', display_name: '欧元区' }
      ];
    
      // arr to obj
    // 输出 {CN: "中国", US: "美国", JP: "日本", EU: "欧元区"}
      const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.display_name;
        return acc
      }, {});

     如果仅仅是提取其中的部分字段,可以这么写:

    formatJson(filterVal, list) {
          return list.map(v => filterVal.map(j => v[j]));
    },
    const filterVal = ['a', 'b'];
    const list = [{a:1,b:2,c:3},{a:5,b:6,c8}];
    const data = this.formatJson(filterVal, list);
  • 相关阅读:
    POJ
    POJ
    POJ
    POJ
    POJ
    ZOJ
    HDU
    python中主要存在的四种命名方式:
    python 中的 赋值 浅拷贝 深拷贝
    python中sorted方法和列表的sort方法使用
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/8759986.html
Copyright © 2011-2022 走看看