Uses: | Ember.MutableEnumerable , |
---|---|
Defined in: | packages/ember-runtime/lib/mixins/mutable_array.js:40 |
Module: | ember |
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:464
Adds an array observer to the receiving array. The array observer object normally must implement two methods:
arrayWillChange(observedObj, start, removeCount, addCount)
- This method will be called just before the array is modified.arrayDidChange(observedObj, start, removeCount, addCount)
- This method will be called just after the array is modified.Both callbacks will be passed the observed object, starting index of the change as well as a count of the items to be removed and added. You can use these callbacks to optionally inspect the array during the change, clear caches, or do any other bookkeeping necessary.
In addition to passing a target, you can also include an options hash which you can use to override the method names that will be invoked on the target.
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:373
Push the object onto the end of the array if it is not already present in the array.
let cities = ['Chicago', 'Berlin']; cities.addObject('Lima'); // ['Chicago', 'Berlin', 'Lima'] cities.addObject('Berlin'); // ['Chicago', 'Berlin', 'Lima']
Inherited from Ember.MutableEnumerable packages/ember-runtime/lib/mixins/mutable_enumerable.js:72
Adds each object in the passed enumerable to the receiver.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:607
Returns true
if the passed function returns true for any item in the enumeration.
The callback method you provide should have the following signature (all parameters are optional):
function(item, index, enumerable);
item
is the current item in the iteration.index
is the current index in the iteration.enumerable
is the enumerable object itself.It should return true
to include the item in the results, false
otherwise.
Note that in addition to a callback, you can also pass an optional target object that will be set as this
on the context. This is a good way to give your iterator function access to the current object.
Usage Example:
if (people.any(isManager)) { Paychecks.addBiggerBonus(); }
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:540
If you are implementing an object that supports Ember.Array
, call this method just after the array content changes to notify any observers and invalidate any related properties. Pass the starting index of the change as well as a delta of the amounts to change.
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:521
If you are implementing an object that supports Ember.Array
, call this method just before the array content changes to notify any observers and invalidate any related properties. Pass the starting index of the change as well as a delta of the amounts to change.
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:81
Remove all elements from the array. This is useful if you want to reuse an existing array without having to recreate it.
let colors = ['red', 'green', 'blue']; colors.length; // 3 colors.clear(); // [] colors.length; // 0
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:770
Returns a copy of the array with all null
and undefined
elements removed.
let arr = ['a', null, 'c', undefined]; arr.compact(); // ['a', 'c']
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:549
Returns true
if the passed function returns true for every item in the enumeration. This corresponds with the every()
method in JavaScript 1.6.
The callback method you provide should have the following signature (all parameters are optional):
function(item, index, enumerable);
item
is the current item in the iteration.index
is the current index in the iteration.enumerable
is the enumerable object itself.It should return the true
or false
.
Note that in addition to a callback, you can also pass an optional target object that will be set as this
on the context. This is a good way to give your iterator function access to the current object.
Example Usage:
if (people.every(isEngineer)) { Paychecks.addBigBonus(); }
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:366
Returns an array with all of the items in the enumeration that the passed function returns true for. This method corresponds to filter()
defined in JavaScript 1.6.
The callback method you provide should have the following signature (all parameters are optional):
function(item, index, enumerable);
item
is the current item in the iteration.index
is the current index in the iteration.enumerable
is the enumerable object itself.It should return true
to include the item in the results, false
otherwise.
Note that in addition to a callback, you can also pass an optional target object that will be set as this
on the context. This is a good way to give your iterator function access to the current object.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:440
Returns an array with just the items with the matched property. You can pass an optional second argument with the target value. Otherwise this will match any property that evaluates to true
.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:474
Returns the first item in the array for which the callback returns true. This method works similar to the filter()
method defined in JavaScript 1.6 except that it will stop working on the array once a match is found.
The callback method you provide should have the following signature (all parameters are optional):
function(item, index, enumerable);
item
is the current item in the iteration.index
is the current index in the iteration.enumerable
is the enumerable object itself.It should return the true
to include the item in the results, false
otherwise.
Note that in addition to a callback, you can also pass an optional target object that will be set as this
on the context. This is a good way to give your iterator function access to the current object.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:532
Returns the first item with a property matching the passed value. You can pass an optional second argument with the target value. Otherwise this will match any property that evaluates to true
.
This method works much like the more generic find()
method.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:241
Iterates through the enumerable, calling the passed function on each item. This method corresponds to the forEach()
method defined in JavaScript 1.6.
The callback method you provide should have the following signature (all parameters are optional):
function(item, index, enumerable);
item
is the current item in the iteration.index
is the current index in the iteration.enumerable
is the enumerable object itself.Note that in addition to a callback, you can also pass an optional target object that will be set as this
on the context. This is a good way to give your iterator function access to the current object.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:292
Alias for mapBy
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:1104
Returns true
if the passed object can be found in the enumerable.
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, undefined].includes(undefined); // true [1, 2, null].includes(null); // true [1, 2, NaN].includes(NaN); // true
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:1104
Returns true
if the passed object can be found in the enumerable.
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, undefined].includes(undefined); // true [1, 2, null].includes(null); // true [1, 2, NaN].includes(NaN); // true
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:374
Returns the index of the given object's first occurrence. If no startAt
argument is given, the starting location to search is 0. If it's negative, will count backward from the end of the array. Returns -1 if no match is found.
let arr = ['a', 'b', 'c', 'd', 'a']; arr.indexOf('a'); // 0 arr.indexOf('z'); // -1 arr.indexOf('a', 2); // 4 arr.indexOf('a', -1); // 4 arr.indexOf('b', 3); // -1 arr.indexOf('a', 100); // -1
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:107
This will use the primitive replace()
method to insert an object at the specified index.
let colors = ['red', 'green', 'blue']; colors.insertAt(2, 'yellow'); // ['red', 'green', 'yellow', 'blue'] colors.insertAt(5, 'orange'); // Error: Index out of range
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:729
Invokes the named method on every object in the receiver that implements it. This method corresponds to the implementation in Prototype 1.6.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:665
Returns true
if the passed property resolves to the value of the second argument for any item in the enumerable. This method is often simpler/faster than using a callback.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:588
Returns true
if the passed property resolves to the value of the second argument for all items in the enumerable. This method is often simpler/faster than using a callback.
Note that like the native Array.every
, isEvery
will return true when called on any empty enumerable.
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:417
Returns the index of the given object's last occurrence. If no startAt
argument is given, the search starts from the last position. If it's negative, will count backward from the end of the array. Returns -1 if no match is found.
let arr = ['a', 'b', 'c', 'd', 'a']; arr.lastIndexOf('a'); // 4 arr.lastIndexOf('z'); // -1 arr.lastIndexOf('a', 2); // 0 arr.lastIndexOf('a', -1); // 4 arr.lastIndexOf('b', 3); // 1 arr.lastIndexOf('a', 100); // 4
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:318
Maps all of the items in the enumeration to another value, returning a new array. This method corresponds to map()
defined in JavaScript 1.6.
The callback method you provide should have the following signature (all parameters are optional):
function(item, index, enumerable);
item
is the current item in the iteration.index
is the current index in the iteration.enumerable
is the enumerable object itself.It should return the mapped value.
Note that in addition to a callback, you can also pass an optional target object that will be set as this
on the context. This is a good way to give your iterator function access to the current object.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:353
Similar to map, this specialized function returns the value of the named property on all items in the enumeration.
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:231
Returns the object at the given index
. If the given index
is negative or is greater or equal than the array length, returns undefined
.
This is one of the primitives you must implement to support Ember.Array
. If your object supports retrieving the value of an array item using get()
(i.e. myArray.get(0)
), then you do not need to implement this method yourself.
let arr = ['a', 'b', 'c', 'd']; arr.objectAt(0); // 'a' arr.objectAt(3); // 'd' arr.objectAt(-1); // undefined arr.objectAt(4); // undefined arr.objectAt(5); // undefined
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:263
This returns the objects at the specified indexes, using objectAt
.
let arr = ['a', 'b', 'c', 'd']; arr.objectsAt([0, 1, 2]); // ['a', 'b', 'c'] arr.objectsAt([2, 3, 4]); // ['c', 'd', undefined]
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:202
Pop object from array or nil if none are left. Works just like pop()
but it is KVO-compliant.
let colors = ['red', 'green', 'blue']; colors.popObject(); // 'blue' console.log(colors); // ['red', 'green']
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:158
Push the object onto the end of the array. Works just like push()
but it is KVO-compliant.
let colors = ['red', 'green']; colors.pushObject('black'); // ['red', 'green', 'black'] colors.pushObject(['yellow']); // ['red', 'green', ['yellow']]
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:179
Add the objects in the passed numerable to the end of the array. Defers notifying observers of the change until all objects are added.
let colors = ['red']; colors.pushObjects(['yellow', 'orange']); // ['red', 'yellow', 'orange']
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:681
This will combine the values of the enumerator into a single value. It is a useful way to collect a summary value from an enumeration. This corresponds to the reduce()
method defined in JavaScript 1.8.
The callback method you provide should have the following signature (all parameters are optional):
function(previousValue, item, index, enumerable);
previousValue
is the value returned by the last call to the iterator.item
is the current item in the iteration.index
is the current index in the iteration.enumerable
is the enumerable object itself.Return the new cumulative value.
In addition to the callback you can also pass an initialValue
. An error will be raised if you do not pass an initial value and the enumerator is empty.
Note that unlike the other methods, this method does not allow you to pass a target object to set as this for the callback. It's part of the spec. Sorry.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:407
Returns an array with all of the items in the enumeration where the passed function returns false. This method is the inverse of filter().
The callback method you provide should have the following signature (all parameters are optional):
function(item, index, enumerable);
It should return a falsey value to include the item in the results.
Note that in addition to a callback, you can also pass an optional target object that will be set as "this" on the context. This is a good way to give your iterator function access to the current object.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:455
Returns an array with the items that do not have truthy values for key. You can pass an optional second argument with the target value. Otherwise this will match any property that evaluates to false.
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:494
Removes an array observer from the object if the observer is current registered. Calling this method multiple times with the same object will have no effect.
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:133
Remove an object at the specified index using the replace()
primitive method. You can pass either a single index, or a start and a length.
If you pass a start and length that is beyond the length this method will throw an OUT_OF_RANGE_EXCEPTION
.
let colors = ['red', 'green', 'blue', 'yellow', 'orange']; colors.removeAt(0); // ['green', 'blue', 'yellow', 'orange'] colors.removeAt(2, 2); // ['green', 'blue'] colors.removeAt(4, 2); // Error: Index out of range
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:345
Remove all occurrences of an object in the array.
let cities = ['Chicago', 'Berlin', 'Lima', 'Chicago']; cities.removeObject('Chicago'); // ['Berlin', 'Lima'] cities.removeObject('Lima'); // ['Berlin'] cities.removeObject('Tokyo') // ['Berlin']
Inherited from Ember.MutableEnumerable packages/ember-runtime/lib/mixins/mutable_enumerable.js:105
Removes each object in the passed enumerable from the receiver.
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:63
Required. You must implement this method to apply this mixin.
This is one of the primitives you must implement to support Ember.Array
. You should replace amt objects started at idx with the objects in the passed array. You should also call this.enumerableContentDidChange()
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:295
Reverse objects in the array. Works just like reverse()
but it is KVO-compliant.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:302
Sets the value on the named property for each member. This is more ergonomic than using other methods defined on this helper. If the object implements Ember.Observable, the value will be changed to set(),
otherwise it will be set directly. null
objects are skipped.
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:314
Replace all the receiver's content with content of the argument. If argument is an empty array receiver will be cleared.
let colors = ['red', 'green', 'blue']; colors.setObjects(['black', 'white']); // ['black', 'white'] colors.setObjects([]); // []
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:228
Shift an object from start of array or nil if none are left. Works just like shift()
but it is KVO-compliant.
let colors = ['red', 'green', 'blue']; colors.shiftObject(); // 'red' console.log(colors); // ['green', 'blue']
Inherited from Ember.Array packages/ember-runtime/lib/mixins/array.js:328
Returns a new array that is a slice of the receiver. This implementation uses the observable array methods to retrieve the objects for the new slice.
let arr = ['red', 'green', 'blue']; arr.slice(0); // ['red', 'green', 'blue'] arr.slice(0, 2); // ['red', 'green'] arr.slice(1, 100); // ['green', 'blue']
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:1044
Converts the enumerable into an array and sorts by the keys specified in the argument.
You may provide multiple arguments to sort by multiple properties.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:754
Simply converts the enumerable into a genuine array. The order is not guaranteed. Corresponds to the method implemented by Prototype.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:818
Returns a new enumerable that contains only unique values. The default implementation returns an array regardless of the receiver type.
let arr = ['a', 'a', 'b', 'b']; arr.uniq(); // ['a', 'b']
This only works on primitive data types, e.g. Strings, Numbers, etc.
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:1075
Returns a new enumerable that contains only items containing a unique property value. The default implementation returns an array regardless of the receiver type.
let arr = [{ value: 'a' }, { value: 'a' }, { value: 'b' }, { value: 'b' }]; arr.uniqBy('value'); // [{ value: 'a' }, { value: 'b' }]
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:253
Unshift an object to start of array. Works just like unshift()
but it is KVO-compliant.
let colors = ['red']; colors.unshiftObject('yellow'); // ['yellow', 'red'] colors.unshiftObject(['black']); // [['black'], 'yellow', 'red']
Defined in packages/ember-runtime/lib/mixins/mutable_array.js:274
Adds the named objects to the beginning of the array. Defers notifying observers until all objects have been added.
let colors = ['red']; colors.unshiftObjects(['black', 'white']); // ['black', 'white', 'red'] colors.unshiftObjects('yellow'); // Type Error: 'undefined' is not a function
Inherited from Ember.Enumerable packages/ember-runtime/lib/mixins/enumerable.js:786
Returns a new enumerable that excludes the passed value. The default implementation returns an array regardless of the receiver type. If the receiver does not contain the value it returns the original enumerable.
let arr = ['a', 'b', 'a', 'c']; arr.without('a'); // ['b', 'c']
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/ember/2.15/classes/Ember.MutableArray/methods