Example 1 — Demo
Core idea.
JavaScript
const a = [1];
const b = { 0: 2, length: 1, [Symbol.isConcatSpreadable]: true };
console.log(a.concat(b).join(","));How It Works
- Flag. Treat like an array for concat.
- Result. Flattened.
Symbol.isConcatSpreadable is a well-known symbol. Whether an object should be flattened by Array.prototype.concat.
const a = [1];
const b = { 0: 2, length: 1, [Symbol.isConcatSpreadable]: true };
console.log(a.concat(b).join(","));Core idea.
const a = [1];
const b = { 0: 2, length: 1, [Symbol.isConcatSpreadable]: true };
console.log(a.concat(b).join(","));CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful