Skip to content

Commit

Permalink
+ array.delete 提高效率
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Dec 27, 2016
1 parent 03ac699 commit db6e99d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "blear.utils.array",
"version": "1.0.10",
"version": "1.0.11",
"description": "数组函数",
"scripts": {
"live": "browser-sync start --config bs-config.js",
Expand Down
59 changes: 31 additions & 28 deletions src/index.js
Expand Up @@ -117,32 +117,19 @@ exports.from = function (obj) {


/**
* 根据元素删除数组元素,会改变原数组
* @param arr {Array} 待删除数组
* @param item {*} 待删除的对象
* @param [deep=false] {Boolean} 是否深度删除,即删除所有匹配项目
* @returns {Array}
* 按索引顺序删除
* @param arr
* @param orderedIndexes
* @returns {*}
*/
exports.delete = function (arr, item, deep) {
var once = function () {
var foundIndex = arr.indexOf(item);

if (foundIndex === -1) {
return false;
}

arr.splice(foundIndex, 1);
};

once();
var remove = function (arr, orderedIndexes) {
var removeLength = 0;

if (deep) {
while (once() !== false) {
//
}
}
each(orderedIndexes, function (_, index) {
arr.splice(index - removeLength, 1);
removeLength++;
});

once = null;
return arr;
};

Expand All @@ -158,14 +145,30 @@ exports.remove = function (arr, indexes) {
return a - b;
});

var removeLength = 0;
return remove(arr, indexes);
};

each(indexes, function (index) {
arr.splice(index - removeLength, 1);
removeLength++;
/**
* 根据元素删除数组元素,会改变原数组
* @param arr {Array} 待删除数组
* @param item {*} 待删除的对象
* @param [deep=false] {Boolean} 是否深度删除,即删除所有匹配项目
* @returns {Array}
*/
exports.delete = function (arr, item, deep) {
var indexes = [];

each(arr, function (index, _item) {
if (item === _item) {
indexes.push(index);

if (!deep) {
return false;
}
}
});

return arr;
return remove(arr, indexes);
};


Expand Down

0 comments on commit db6e99d

Please sign in to comment.