JS Tricks

Different ways. of creating array

By Pawan on 2 weeks ago|0 comment||
0

A few techniques to create an array

// 1. Array Literal Syntax
const fruits = ['apple', 'banana', 'cherry'];

// 2. Using the new Array() constructor
const numbers = new Array(1, 2, 3, 4, 5);

// 3. Using the Array.from() method
const squares = Array.from({ length: 5 }, (_, index) => index * index);

// 4. Using Array.of()
const elements = Array.of(1, 'hello', true, { key: 'value' });

// 5. Using Array.prototype.push()
const colors = [];
colors.push('red', 'green', 'blue');

// 6. Using Array.prototype.concat()
const veggies = ['carrot', 'broccoli'];
const fruits = ['apple', 'banana'];
const food = veggies.concat(fruits);

// 7. Using Array.prototype.slice()
const animals = ['lion', 'tiger', 'bear'];
const animalsSubset = animals.slice(1, 3); // ['tiger', 'bear']

// 8. Using the fill() method
const emptyArray = new Array(5).fill(0);

// 9. Using Array Destructuring
const [first, second, third] = ['one', 'two', 'three'];

// 10. Using Array.prototype.map()
const numbers = [1, 2, 3];
const squaredNumbers = numbers.map(num => num * num);

// 11. Using Array.prototype.filter()
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);

// 12. Using Array.prototype.reduce()
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);

// 13. Using Array Comprehensions (ECMAScript 7 feature)
const numbers = [for (i of [1, 2, 3]) i * i];

// 14. Using the Array() constructor with a single numeric argument
const emptyArray = new Array(10); // creates an array with 10 empty slots

// 15. Using Array.prototype.splice()
const fruits = ['apple', 'banana', 'cherry'];
fruits.splice(1, 0, 'orange'); // adds 'orange' at index 1

// 16. Using Array Spread Syntax
const firstArray = [1, 2, 3];
const secondArray = [...firstArray, 4, 5, 6];

// 17. Using Array.prototype.unshift()
const numbers = [3, 4, 5];
numbers.unshift(1, 2); // adds 1 and 2 to the beginning

// 18. Using Array.prototype.fill() with a value
const newArray = new Array(5).fill('hello');

// 19. Using Array.prototype.keys() to get an iterator for array keys
const fruits = ['apple', 'banana', 'cherry'];
const keysIterator = fruits.keys();

// 20. Using Array.prototype.values() to get an iterator for array values
const fruits = ['apple', 'banana', 'cherry'];
const valuesIterator = fruits.values();

Add a Comment

Comments

Comments MIA, jokes needed ASAP. Add yours! 😄

What is JavaScript Hacks? 🚀

Hey there, fellow code adventurer! Ever wished JavaScript could be more than just lines of serious code? Well, welcome to JS Hacks – where JavaScript gets a playful makeover! 🎉

So, whether you're a coding newbie or a seasoned pro, join us on this epic quest to discover the quirkiest, coolest, and downright silliest JavaScript hacks out there. Trust us, your code will thank you (and maybe even crack a smile). 😄

Ready to hack, slash, and LOL your way through JavaScript? Let's dive in and unleash the fun-tastic power of JS Hacks together! 💻✨


DISCOVER

ENGAGE

Add Trick