How to get distinct values from an array of objects in JavaScript?

前端 未结 30 2622
执笔经年
执笔经年 2020-11-22 05:29

Assuming I have the following:

var array = 
    [
        {\"name\":\"Joe\", \"age\":17}, 
        {\"name\":\"Bob\", \"age\":17}, 
        {\"name\":\"Carl\         


        
30条回答
  •  礼貌的吻别
    2020-11-22 06:03

    i think you are looking for groupBy function (using Lodash)

    _personsList = [{"name":"Joe", "age":17}, 
                    {"name":"Bob", "age":17}, 
                    {"name":"Carl", "age": 35}];
    _uniqAgeList = _.groupBy(_personsList,"age");
    _uniqAges = Object.keys(_uniqAgeList);
    

    produces result:

    17,35
    

    jsFiddle demo:http://jsfiddle.net/4J2SX/201/

提交回复
热议问题