Skip to content
the poor coder

convert an array to other array

Poor Man
Published
Reading time
2 min read
On this page

Convert an array to other array#

Converting an array to another array means creating a new array from an existing array with modifications or changes. There are different ways to achieve this, depending on the programming language and specific requirements.

Using map() function#

The map() function is a built-in method in JavaScript that creates a new array by calling a provided function on each element of the original array. The provided function can modify the elements as needed.

Text

const originalArray = [1, 2, 3, 4, 5];
const modifiedArray = originalArray.map(element => element * 2);
console.log(modifiedArray); // [2, 4, 6, 8, 10]

The above code creates a new array by multiplying each element of the original array by 2. The result is stored in the modifiedArray variable.

Using filter() function#

The filter() function is another built-in method in JavaScript that creates a new array by filtering elements from the original array based on a provided condition. The condition should return true or false for each element.

Text

const originalArray = [1, 2, 3, 4, 5];
const filteredArray = originalArray.filter(element => element % 2 === 0);
console.log(filteredArray); // [2, 4]

The above code creates a new array by filtering out odd numbers from the original array. The result is stored in the filteredArray variable.

Using slice() function#

The slice() function is a built-in method in JavaScript that creates a new array by extracting a portion of the original array. The portion is specified by the start and end indexes. The end index is optional and defaults to the length of the array.

Text

const originalArray = [1, 2, 3, 4, 5];
const slicedArray = originalArray.slice(1, 4);
console.log(slicedArray); // [2, 3, 4]

The above code creates a new array by extracting elements from the original array between the indexes 1 and 4 (exclusive). The result is stored in the slicedArray variable.

Using concat() function#

The concat() function is a built-in method in JavaScript that creates a new array by concatenating two or more arrays. The original arrays are not modified.

Text

const originalArray1 = [1, 2];
const originalArray2 = [3, 4];
const concatenatedArray = originalArray1.concat(originalArray2);
console.log(concatenatedArray); // [1, 2, 3, 4]

The above code creates a new array by concatenating two original arrays into one. The result is stored in the concatenatedArray variable.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC