JavaScript/array:修订间差异
| 第456行: | 第456行: | ||
// if ((item.name?.indexOf(searchValue) != -1)) return 1 === 1; | // if ((item.name?.indexOf(searchValue) != -1)) return 1 === 1; | ||
});</syntaxhighlight> | });</syntaxhighlight> | ||
== 参考 == | |||
* [https://v8.dev/blog/elements-kinds Elements kinds in V8] | |||
2022年4月26日 (二) 07:41的版本
引言
特点
- 相同的数据类型
- 连续的线性内存
稀疏数组
有空值的数组。
方法
| find | forEach | map |
| every | some | filter |
| indexOf | reverse | 解构... |
holes
数组空洞(缺失索引)[1]。
示例
创建数组
new
apply
Array.from
fill
插入
合并
合并两个数组。 模板:Leftrightflex
交换
第三方变量
const temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
splice
array.splice(index2,1,...array.splice(index1, 1 , array[index2]));
解构赋值
[array[index1],array[index2]] = [array[index2],array[index1]];
查找
优化
二分法
O(log n)
删除
splice
删除。 模板:Leftrightflex 替换。 模板:Leftrightflex 添加。 模板:Leftrightflex
delete
concat
读取
遍历
判断
if (!this.length || this.length == 0) return this;
示例
去重
Array.prototype.uniq = function () {
if (!this.length || this.length == 0) return this;
var res = [], key, hasNaN = false, temp = {};
for (var i = 0 ; i < this.length; i++) {
if (typeof this[i] === 'object') {
res.push(this[i]);
} else if (this[i] != this[i]) { // 如果当前遍历元素是NaN
if (!hasNaN) {
res.push(this[i]);
hasNaN = true;
}
} else {
key = typeof(this[i]) + this[i];
if (!temp[key]) {
res.push(this[i]);
temp[key] = true;
}
}
}
return res;
}
Array.prototype.uniq = function () {
var res = [];
var flag = true;
this.forEach(function(x) {
if (res.indexOf(x) == -1) {
if (x != x) {
if (flag) {
res.push(x);
flag = false;
}
} else {
res.push(x);
}
}
})
return res;
}
数组相减
a - b
数组取不相同部分
数组拍平
样本。
const sample = [1, 2, [3, 4, 5, [6, 7, 8], 9], 10, [11, 12]];
递归。 模板:Leftrightflex
reduce。 模板:Leftrightflex
flat。 模板:Leftrightflex
扩展运算符。有待优化。 模板:Leftrightflex
toString。 模板:Leftrightflex
apply。 模板:Leftrightflex
清空数组
清空数组
相关问题
今天(2021-1-18)视频面试美团的一个岗位,笔试题是数组去重,如输入 [1, 2, 1, 1, '1'],输出 [1, 2, '1' ],看到题目第一眼我就想起使用 new Set,然后写了如下代码:
...new Set(array)
模板:Leftrightflex
接着试探着问面试官应该可以吧,面试官反应平平(我猜测他可能没用过这种方式,也不知道对不对),我也不好再尝试运行,虽然我知道可以解这道题,但是面试官不确定那我还是老老实实写其他代码吧,然后写了如下:
模板:Leftrightflex
面试官说你看对么,然后我看了一眼发现 Number 1 和 String 1 被等值了,毕竟是 map 是一个 object。我又尝试的问了一下,您觉的这样应该怎么处理呢,面试官说:“你在里面加一下判断,试着把 Number 1 和 String 1 区分开”,我想了一下没什么概念就试探着说其实这里可以用 Map 解决的,只需要把 var map = new Map() 就可以了(我感觉没有我反应快。。。,而且这判断怎么加。。。),面试官无感,我也就没再改代码,然后结束了。事后补全以下代码:
过滤
多层过滤(多维过滤)
数组多层过滤,需要注意引用的问题。
样本数据。
[{
"id": 3541,
"name": "124",
"create_at": "2021-12-26 21:57:41",
"group": "124",
"artboards": [{
"id": 32211,
"name": "one",
"checked": false
}],
"expend": true
}, {
"id": "null",
"group": "未分组",
"artboards": [{
"id": 32131,
"name": "Toast_icon_prompt@2x",
"checked": false
}, {
"id": 32130,
"name": "Toast_icon_prompt@3x",
"checked": false
}],
"name": "未分组"
}]
JSON
JSON.parse(JSON.stringify(foo));
if (''.indexOf(searchValue) > -1) return setCurrentList(groupList);
const groupListTmp = JSON.parse(JSON.stringify(groupList));
const tmpList = groupListTmp.filter(item => {
if (item?.artboards?.length) {
const queryChildrenNode = item?.artboards.filter(item => (item.name.indexOf(searchValue) != -1));
if (queryChildrenNode.length) {
item.artboards = queryChildrenNode;
return 1 === 1;
}
}
// if ((item.name?.indexOf(searchValue) != -1)) return 1 === 1;
});
参考
No categories