Get unique keys from multiple objects

后端 未结 3 388
不思量自难忘°
不思量自难忘° 2020-12-11 12:45

If I have an array of objects like this:

var mountains = [
    { name: \'Kebnekaise\', elevation: 2106 },
    { name: \'Mount Ngauruhoe\', elevation: 2291, c         


        
3条回答
  •  心在旅途
    2020-12-11 13:22

    Using ES6, one could do

    var unique = new Set([].concat.apply([],mountains.map(Object.keys)))
    

    Without ES6, something like

    var unique = [].concat.apply([],mountains.map(Object.keys)).filter(function(value,i,arr) {
        return arr.indexOf(value) === i;
    });
    

提交回复
热议问题