数组的map方法

map和forEach都用于遍历数组,forEach没有返回值。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+100return json
    });
    console.log(newArr)
 
 
</script>

返回值:

[
    {t:'aaa',r:150},
    {t:'bbb',r:150},
    {t:'ccc',r:150}
 
]
原文地址:https://www.cnblogs.com/jervy/p/13617573.html