zoukankan      html  css  js  c++  java
  • es6语法---从提交的数据 找到对应list里面的 键值id 替换--find

    比对的数据:
    targetlist=[ { "id": 1, "host": "https://test-api-crm-codemaster.codemao.cn/" }, { "id": 2, "host": "https://test-api-marketing.codemao.cn/" } ]
     
    提交的数据:
    values=
    {

    case_name: "1111",
    group_id: 23,
    methods: "get",
    re_head: "1111",
    re_path: "1111",
    st_code: "111",
    st_host: "https://test-api-crm-codemaster.codemao.cn/",
    state: ""
    }

    需求: 

    提交的st_host 字符 找到 targetlist对应的id 替换

    const newValues = {...values} // 解构values,将原form的values形成一个新的对象newValues
    newValues.st_host = this.hosts.find(t => t.id === Number(values.st_host)).host // 利用es6数组的find函数找到对应id的item,将该item的host赋值给st_host
     
    红色那坨等于下面的写法:
    let host = ''
    this.hosts.forEach((item, index) => {
    if (item.id === Number(values.st_host)) {
      host = item.host
     }
    })
     
    等于下面这坨

    var inventory = [
    {name: 'apples', quantity: 2},
    {name: 'bananas', quantity: 0},
    {name: 'cherries', quantity: 5}
    ];

    function findCherries(fruit) {
    return fruit.name === 'cherries';
    }

    console.log(inventory.find(findCherries));

    https://www.cnblogs.com/amujoe/p/8875053.html

    https://www.cnblogs.com/ChineseLiao/p/11638175.html

  • 相关阅读:
    假期第九天
    假期第八天
    8.9周总结
    构建之法阅读笔记之三
    构建之法阅读笔记之二
    构建之法阅读笔记之一
    03人月神话阅读笔记之一
    手机输入法使用评价
    十天冲刺-第十天
    十天冲刺-第九天
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/14136362.html
Copyright © 2011-2022 走看看