es6新增方法map()

通常用法爲:

  • 用法爲:arr.map()
    • 這個方法常常被用來作數據交互映射
    • 返回值爲一個新的數組,數組中的元素爲原始數組元素通過處理後的值
  • 注意:map()不會對空數組檢測
  • map()不會改變原始數據
  • 小例子
<script>
    let arr = [
        {title:'aaa',read:50},
        {title:'bbb',read:50},
        {title:'ccc',read:50}
    ];
    let newArr = arr.map((item, index, arr)=>{
        let json = {}
        json.t = `${item.title}`;
        json.r = item.read+100;
        return json
    });
    console.log(newArr)


</script>
複製代碼

輸出結果爲:

[
    {t:'aaa',r:150},
    {t:'bbb',r:150},
    {t:'ccc',r:150}

]
複製代碼

若有錯誤請指出

相關文章
相關標籤/搜索