JavaScript Array to Set

后端 未结 4 1362
时光说笑
时光说笑 2020-12-23 23:46

MSDN references JavaScript\'s Set collection abstraction. I\'ve got an array of objects that I\'d like to convert to a set so that I am able to remove (.delete()

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 00:32

    If you start out with:

    let array = [
        {name: "malcom", dogType: "four-legged"},
        {name: "peabody", dogType: "three-legged"},
        {name: "pablo", dogType: "two-legged"}
    ];
    

    And you want a set of, say, names, you would do:

    let namesSet = new Set(array.map(item => item.name));
    

提交回复
热议问题