Function

Module: @ember/object
propertyKey
String
returns
ComputedProperty
computes a new array with all the duplicated elements from the dependent arrays

A computed property which returns a new array with all the elements two or more dependent arrays have in common.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { set } from '@ember/object';
import { intersect } from '@ember/object/computed';

class FriendGroups {
  constructor(adaFriends, charlesFriends) {
    set(this, 'adaFriends', adaFriends);
    set(this, 'charlesFriends', charlesFriends);
  }

  @intersect('adaFriends', 'charlesFriends') friendsInCommon;
}

let groups = new FriendGroups(
  ['Charles Babbage', 'John Hobhouse', 'William King', 'Mary Somerville'],
  ['William King', 'Mary Somerville', 'Ada Lovelace', 'George Peacock']
);

groups.friendsInCommon; // ['William King', 'Mary Somerville']

Classic Class Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
import EmberObject from '@ember/object';
import { intersect } from '@ember/object/computed';

let FriendGroups = EmberObject.extend({
  friendsInCommon: intersect('adaFriends', 'charlesFriends')
});

let groups = FriendGroups.create({
  adaFriends: ['Charles Babbage', 'John Hobhouse', 'William King', 'Mary Somerville'],
  charlesFriends: ['William King', 'Mary Somerville', 'Ada Lovelace', 'George Peacock']
});

groups.friendsInCommon; // ['William King', 'Mary Somerville']