假设我有以下内容:

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

获得所有不同年龄的数组的最佳方法是什么,这样我就得到了一个结果数组:

[17, 35]

是否有一些方法,我可以选择结构数据或更好的方法,这样我就不必遍历每个数组检查“年龄”的值,并检查另一个数组是否存在,如果没有添加它?

如果有某种方法可以让我不用迭代就能得到不同的年龄……

目前效率低下的方式,我想改进…如果它的意思不是“数组”是一个对象的数组,而是一个对象的“映射”与一些唯一的键(即。"1,2,3")也可以。我只是在寻找最高效的方式。

以下是我目前的做法,但对我来说,迭代似乎只是为了提高效率,即使它确实有效……

var distinct = []
for (var i = 0; i < array.length; i++)
   if (array[i].age not in distinct)
      distinct.push(array[i].age)

当前回答

清洁解决方案

export abstract class Serializable<T> {
  equalTo(t: Serializable<T>): boolean {
    return this.hashCode() === t.hashCode();
  }
  hashCode(): string {
    throw new Error('Not Implemented');
  }
}

export interface UserFields {
  firstName: string;
  lastName: string;
}

export class User extends Serializable<User> {
  constructor(private readonly fields: UserFields) {
    super();
  }
  override hashCode(): string {
    return `${this.fields.firstName},${this.fields.lastName}`;
  }
}

const list: User[] = [
  new User({ firstName: 'first', lastName: 'user' }),
  new User({ firstName: 'first', lastName: 'user' }),
  new User({ firstName: 'second', lastName: 'user' }),
  new User({ firstName: 'second', lastName: 'user' }),
  new User({ firstName: 'third', lastName: 'user' }),
  new User({ firstName: 'third', lastName: 'user' }),
];

/**
 * Let's create an map
 */
const userHashMap = new Map<string, User>();


/**
 * We are adding each user into the map using user's hashCode value
 */
list.forEach((user) => userHashMap.set(user.hashCode(), user));

/**
 * Then getting the list of users from the map,
 */
const uniqueUsers = [...userHashMap.values()];


/**
 * Let's print and see we did right?
 */
console.log(uniqueUsers.map((e) => e.hashCode()));

其他回答

原始类型

var unique = [...new Set(array.map(item => item.pritiveAttribute))];

对于复杂类型,例如对象

var unique = [...new DeepSet(array.map(item => item.Object))];

export class DeepSet extends Set {

  add (o: any) {
    for (let i of this)
      if (this.deepCompare(o, i))
        return this;
    super.add.call(this, o);
    return this;
  };

  private deepCompare(o: any, i: any) {
    return JSON.stringify(o) === JSON.stringify(i)
  }
}

刚找到这个,我觉得很有用

_.map(_.indexBy(records, '_id'), function(obj){return obj})

还是用下划线,如果你有一个这样的对象

var records = [{_id:1,name:'one', _id:2,name:'two', _id:1,name:'one'}]

它只会给你唯一的对象。

这里发生的是indexBy返回一个像这样的映射

{ 1:{_id:1,name:'one'}, 2:{_id:2,name:'two'} }

因为它是一个映射,所有的键都是唯一的。

然后我把这个列表映射回数组。

如果你只需要不同的值

_.map(_.indexBy(records, '_id'), function(obj,key){return key})

请记住,键是作为字符串返回的,因此,如果您需要整数,您应该这样做

_.map(_.indexBy(records, '_id'), function(obj,key){return parseInt(key)})

Const数组= [ {" id ": " 93 ", "名称":" CVAM_NGP_KW "}, {" id ": " 94 ", "名称":" CVAM_NGP_PB "}, {" id ": " 93 ", "名称":" CVAM_NGP_KW "}, {" id ": " 94 ", "名称":" CVAM_NGP_PB "} ] 函数uniq(数组,字段){ 返回数组中。Reduce((累加器,电流)=> { 如果(! accumulator.includes(当前(领域))){ accumulator.push(当前(领域)) } 返回蓄电池; }, [] ) } Const id = uniq(数组,'id'); console.log (ids) / *输出 (“93”,“94”) * /

使用d3.js v3:

  ages = d3.set(
    array.map(function (d) { return d.age; })
  ).values();

以防你需要整个对象的唯一性

const _ = require('lodash');

var objects = [
  { 'x': 1, 'y': 2 },
  { 'y': 1, 'x': 2 },
  { 'x': 2, 'y': 1 },
  { 'x': 1, 'y': 2 }
];

_.uniqWith(objects, _.isEqual);

[对象{x: 1, y: 2},对象{x: 2, y: 1}]