I always forget about Array.flat(). There's definitely some Array.isArray(val) ? [...a, ...val] : [...a, val] lines that I could simplify with it. :)
Array.flat()
Array.isArray(val) ? [...a, ...val] : [...a, val]
Array.concat() does a similar job, it´s just a matter of taste:
let a = [1,2,3,4] console.log(a.concat(5,6)) // -> [ 1, 2, 3, 4, 5, 6 ]
It is amazing what is already implemented in the Javascript Arrays. See this overview:
Array.prototype[@@iterator]() Array.prototype.at() Array.prototype.concat() Array.prototype.copyWithin() Array.prototype.entries() Array.prototype.every() Array.prototype.fill() Array.prototype.filter() Array.prototype.find() Array.prototype.findIndex() Array.prototype.findLast() Array.prototype.findLastIndex() Array.prototype.flat() Array.prototype.flatMap() Array.prototype.forEach() Array.from() Array.fromAsync() Array.prototype.includes() Array.prototype.indexOf() Array.isArray() Array.prototype.join() Array.prototype.keys() Array.prototype.lastIndexOf() Array.prototype.map() Array.of() Array.prototype.pop() Array.prototype.push() Array.prototype.reduce() Array.prototype.reduceRight() Array.prototype.reverse() Array.prototype.shift() Array.prototype.slice() Array.prototype.some() Array.prototype.sort() Array.prototype.splice() Array.prototype.toLocaleString() Array.prototype.toReversed() Array.prototype.toSorted() Array.prototype.toSpliced() Array.prototype.toString() Array.prototype.unshift() Array.prototype.values() Array.prototype.with()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I always forget about
Array.flat(). There's definitely someArray.isArray(val) ? [...a, ...val] : [...a, val]lines that I could simplify with it. :)Array.concat() does a similar job, it´s just a matter of taste:
It is amazing what is already implemented in the Javascript Arrays. See this overview: