Skip to content

25.什么是Array map() 方法 #25

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
webVueBlog opened this issue Mar 11, 2020 · 1 comment
Open

25.什么是Array map() 方法 #25

webVueBlog opened this issue Mar 11, 2020 · 1 comment
Labels
JavaScript JavaScript

Comments

@webVueBlog
Copy link
Member

No description provided.

@webVueBlog webVueBlog added the JavaScript JavaScript label Mar 11, 2020
@webVueBlog
Copy link
Member Author

实例

返回一个数组,数组中元素为原始数组的平方根:

var numbers = [4, 9, 16, 25];

function myFunction() {
    x = document.getElementById("demo")
    x.innerHTML = numbers.map(Math.sqrt);
}

输出结果为:

2,3,4,5

定义和用法

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

map() 方法按照原始数组元素顺序依次处理元素。

注意: map() 不会对空数组进行检测。

注意: map() 不会改变原始数组。

接口数据映射

从接口得到数据 res:

let r = res.map(item => {
    return {
        title: item.name,
        sex: item.sex === 1? '男':item.sex === 0?'女':'保密',
        age: item.age,
        avatar: item.img
    }
})

也可以省略return

const users=res.items.map(item => ({
    url: item.html_url,      
    img: item.avatar_url,      
    name: item.login,
    })
);

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
JavaScript JavaScript
Projects
None yet
Development

No branches or pull requests

1 participant