如何用javascript比较多个数组,在每个数组中只得到相同时间的结果
How to compare multiple array get result only same time in each array with javascript?
如何用javascript比较多个数组,在每个数组中只得到相同时间的数组结果?考虑性能
var a = [10],
b = [],
c = [10],
d = [10];
get []
var a = [10],
b = [10],
c = [10],
d = [10];
get [10]
var a = [0, 1, 2],
b = [1, 2],
c = [0, 1, 2],
d = [0, 1, 2, 3];
get [1, 2]
此解决方案的特点是使用非破坏性算法来获取两个数组(intersection
)和一组数组(intersections
)的交集。
function intersection(a, b) {
var array = [];
a.forEach(function (aa) {
b.some(function (bb) {
if (aa === bb) {
array.push(aa);
return true;
}
});
});
return array;
}
function intersections(array) {
return array.reduce(function (a, b) {
return intersection(a, b);
});
}
document.write('<pre>' + JSON.stringify(intersections([[10], [], [10], [10]]), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(intersections([[10], [10], [10], [10]]), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(intersections([[0, 1, 2], [1, 2], [0, 1, 2], [0, 1, 2, 3]]), 0, 4) + '</pre>');
_.交集是单向的:)
_.intersection([1, 2], [4, 2], [2, 1]);
// → [2]
参见Lodash。如果你的问题更多是出于实用而非学术原因,这是有用的。没有必要重新发明轮子,除非只是为了学习。
相关文章:
- knex:根据结果创建数组的合适方法是什么
- Mongodb$in以与数组中相同的顺序获取结果
- 使用ScriptObjectMirror将数组结果从JavaScript转换为有用的东西
- 从重复的javascript数组结果集中只获取一行
- 如何从合并的结果集中提取数组
- JSON返回数组的奇怪结果
- JavaScript-将for循环的多个结果推送到数组中
- 从source-mirth检索通道目标中的数组结果
- json_encode 的 mysqli 查询奇怪的数组结果
- 茉莉花期望不匹配数组结果
- 用promise处理promise的数组结果
- 根据id表将单个数组结果发送到打印机
- 如何获得Javascript数组结果到Java
- 显示输入文本的数组结果
- Javascript删除数组结果留下一个逗号
- 如何推翻数组结果树从子-父到父-子在Javascript
- 将数组结果过滤到下拉列表中
- Javascript数组结果返回undefined
- Javascript:标记数组结果
- 如何将数组结果视为数组Javascript