Skip to content
the poor coder

function data_map() { var data = [ ['a','b', 1], ['c','d', 2], ['e','f', 3], ['g','h', 4] ] var newData = data.map(function(row){ return [row[0], ro

Poor Man
Published
Reading time
1 min read
On this page

Function data_map()#

JavaScript

var data = [ 
    ['a','b', 1], 
    ['c','d', 2], 
    ['e','f', 3], 
    ['g','h', 4] 
];

var newData = data.map(function(row){ 
    return [row[0], row[2]]; 
});

The data_map() function creates a new array, newData, by mapping each element from the original array, data. Each element in the data array is itself an array of three elements. The first two elements are strings, and the third element is a number.

The Callback Function#

The callback function used in the map() method takes a single argument, row, which represents the current element from the data array being processed. The function then returns a new array containing only the first and third elements of the current row.

JavaScript

var newData = data.map(function(row){ 
    return [row[0], row[2]]; 
});

Alternative Ways to Achieve the Same Result#

Another way to achieve the same result is by using arrow function syntax instead of function declaration syntax. This would look like:

JavaScript

var newData = data.map((row) => [row[0], row[2]]);

Additionally, one could use destructuring assignment to directly extract the first and third elements of each row into a new array. This would look like:

JavaScript

var newData = data.map(([first, second, third]) => [first, third]);

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