Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

reduce的使用 #4

Open
DongLee0504 opened this issue Oct 18, 2018 · 0 comments
Open

reduce的使用 #4

DongLee0504 opened this issue Oct 18, 2018 · 0 comments
Labels

Comments

@DongLee0504
Copy link
Owner

提取对象的某些属性

let res = {
        name: 'lidong',
        age: 18,
        sports: 'baseketball'
    },
    obj,
    arr = ['name'];
    obj = arr.reduce((p, n) => {
        if(n in res) {
            p[n] = res[n]
        };
        return p
    },{})
    console.log(obj); // {name: "lidong"}

计算出一个字符串中每个字符出现的次数

let str = 'hello world';
    let obj2 = str.split('').reduce((acc, item) => {
        acc[item] = (acc[item] || 0) + 1;
        return acc
    }, {});
    console.log(obj2) // {h: 1, e: 1, l: 3, o: 2, " ": 1, …}
@DongLee0504 DongLee0504 added the js label Oct 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant