add Ox.remove: sugar for array.splice(array.indexOf(element), 1)
This commit is contained in:
parent
03850ee600
commit
4b01e4984b
1 changed files with 35 additions and 0 deletions
|
@ -343,6 +343,41 @@ Ox.min = function(collection) {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*@
|
||||||
|
Ox.remove <f> Removes an element from an array or object and returns it
|
||||||
|
(collection, element) -> <*> Element, or undefined if not found
|
||||||
|
<script>
|
||||||
|
Ox.test.collection = [
|
||||||
|
['a', 'b', 'c'],
|
||||||
|
{a: 0, b: 1, c: 2}
|
||||||
|
];
|
||||||
|
</script>
|
||||||
|
> Ox.remove(Ox.test.collection[0], 'b')
|
||||||
|
'b'
|
||||||
|
> Ox.remove(Ox.test.collection[1], 1)
|
||||||
|
1
|
||||||
|
> Ox.remove(Ox.test.collection[1], 3)
|
||||||
|
void 0
|
||||||
|
> Ox.test.collection
|
||||||
|
[['a', 'c'], {a: 0, c: 2}]
|
||||||
|
@*/
|
||||||
|
Ox.remove = function(collection, element) {
|
||||||
|
var ret, key;
|
||||||
|
if (Ox.isArray(collection)) {
|
||||||
|
key = collection.indexOf(element);
|
||||||
|
if (key > -1) {
|
||||||
|
ret = collection.splice(key, 1)[0];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
key = Ox.keyOf(collection, element);
|
||||||
|
if (key) {
|
||||||
|
ret = collection[key];
|
||||||
|
delete collection[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.reverse <f> Reverses an array or string
|
Ox.reverse <f> Reverses an array or string
|
||||||
> Ox.reverse([1, 2, 3])
|
> Ox.reverse([1, 2, 3])
|
||||||
|
|
Loading…
Reference in a new issue