[英]How to spread an array without using a spread operator?
Is there a way through functions or array methods to spread an array without the spread operator.有没有办法通过函数或数组方法在没有扩展运算符的情况下扩展数组。 I was solving Algo challenges on FCC and was just curious about this.我正在解决 FCC 上的 Algo 挑战,并且对此很好奇。
function uniteUnique(arr) {
var args = [].slice.call(arguments);
var newArr = [];
args.forEach(arr => newArr.push(...arr));
return newArr.reduce((array, num) => {
if(!array.includes(num)) {
array.push(num);
}
return array;
}, []);
}
var args = [].slice.call(arguments);
var newArr = [];
args.forEach(arr => newArr.push(...arr));
this part of your code is just flattening the arguments by one level, and can also be written as这部分代码只是将参数扁平化一层,也可以写成
var newArr = [].concat.apply([], arguments);
and和
return newArr.reduce((array, num) => {
if(!array.includes(num)) {
array.push(num);
}
return array;
}, []);
is returning a version of that without duplicates.正在返回一个没有重复的版本。 Wich can also be achieved differently也可以以不同的方式实现
//adding that, because it's so much simpler than your reduce
return [...new Set(newArr)];
return Array.from(new Set(newArr));
//or
return newArr.filter(function(v,i){ return newArr.indexOf(v) === i });
and the whole thing和整件事
function uniteUnique( /*...arrays*/ ) { return [].concat.apply([], arguments) .filter(function(v, i, a) { return a.indexOf(v) === i }); } console.log(uniteUnique( 1, [2, 3, 4], 2, [3, 5], [2, 4, 6, 8], 8, [1, 3, 5, 7, 9] ));
.as-console-wrapper{top:0;max-height:100%!important}
If you want to flatten by multiple (all) levels, you can do this like如果您想通过多个(所有)级别展平,您可以这样做
//reusable helper
function _flatten(into, value){
if(Array.isArray(value))
return value.reduce(_flatten, into);
into.push(value);
return into;
}
function _unique(v,i,a){
return a.indexOf(v) === i;
}
function uniteUnique( /*...arrays*/ ) {
return _flatten([], [].slice.call(arguments))
.filter(_unique);
}
or the all-in-one solution或多合一解决方案
function uniteUnique( /*...arrays*/ ) {
return (function _recursive(into, value){
if(Array.isArray(value))
return value.reduce(_recursive, into);
if(into.indexOf(value) === -1) into.push(value);
return into;
})([], [].slice.call(arguments));
}
const newArray = [...oldArray];
will produce identical output as using Array.prototype.concat :将产生与使用Array.prototype.concat相同的输出:
const newArray = [].concat(oldArray)
if you want to merge all arrays into one then you can do it with Array.prototype.reduce :如果您想将所有数组合并为一个,那么您可以使用Array.prototype.reduce 来完成:
const newArray = args.reduce((acc, arr) => acc.concat(arr), [])
Use Function.prototype.apply()
.使用Function.prototype.apply()
。 Consider this article about emulating spread with apply()
: Using 'apply' to Emulate JavaScript's Upcoming Spread Operator .考虑这篇关于使用apply()
模拟传播的文章: 使用 'apply' 来模拟 JavaScript 即将推出的传播运算符。
function uniteUnique(arr) {
var args = [].slice.call(arguments);
var newArr = [];
args.forEach(arr => Array.prototype.push.apply(newArr, arr));
return newArr.reduce((array, num) => {
if(!array.includes(num)) {
array.push(num);
}
return array;
}, []);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.