You can do it with .map(). Immagine a fetch of database rows.
With arrow function
To have a reusable arrayColumn(array, column) function:
const array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
const arrayColumn = (array, column) => {
return array.map(item => item[column]);
};
const names = arrayColumn(array, 'name');
console.log(names);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Or you can use .map directly:
const array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
const names = array.map(item => item.name);
console.log(names);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Before ES6 (2015)
var array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
function arrayColumn(array, columnName) {
return array.map(function(value,index) {
return value[columnName];
})
}
var names = arrayColumn(array, 'name');
console.log(names);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from fabpico on Stack OverflowYou can do it with .map(). Immagine a fetch of database rows.
With arrow function
To have a reusable arrayColumn(array, column) function:
const array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
const arrayColumn = (array, column) => {
return array.map(item => item[column]);
};
const names = arrayColumn(array, 'name');
console.log(names);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Or you can use .map directly:
const array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
const names = array.map(item => item.name);
console.log(names);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Before ES6 (2015)
var array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
function arrayColumn(array, columnName) {
return array.map(function(value,index) {
return value[columnName];
})
}
var names = arrayColumn(array, 'name');
console.log(names);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Shorter version of Fabian's answer using arrow notation:
let array = [
{
id: 1,
name: 'foo'
},
{
id: 2,
name: 'bar'
},
];
let names = array.map( el => el.name );
javascript - Get column from a two dimensional array - Stack Overflow
javascript - Get column in array of arrays - Stack Overflow
Column of a matrix [array]
add new column in javascript array - Stack Overflow
Taking a column is easy with the map function.
// a two-dimensional array
var two_d = [[1,2,3],[4,5,6],[7,8,9]];
// take the third column
var col3 = two_d.map(function(value,index) { return value[2]; });
Why bother with the slice at all? Just filter the matrix to find the rows of interest.
var interesting = two_d.filter(function(value,index) {return value[1]==5;});
// interesting is now [[4,5,6]]
Sadly, filter and map are not natively available on IE9 and lower. The MDN documentation provides implementations for browsers without native support.
Use Array.prototype.map() with an arrow function:
const arrayColumn = (arr, n) => arr.map(x => x[n]);
const twoDimensionalArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
console.log(arrayColumn(twoDimensionalArray, 0));
Note: Array.prototype.map() and arrow functions are part of ECMAScript 6 and not supported everywhere, see ECMAScript 6 compatibility table.
The “fastest” in terms of “least code” would probably be Array.prototype.map:
const getColumn = (anArray, columnNumber) =>
anArray.map(row => row[columnNumber]);
const getColumn = (anArray, columnNumber) =>
anArray.map(row => row[columnNumber]);
const arr = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
console.log(getColumn(arr, 0));
Below is a quick example i think:
var column_number = 2;
var column = [];
for(var i=0; i<9; i++) {
var value = matrix[i][column_number];
column.push(value);
}
You could iterate though the data array and add key & value to each element.
data[0]["foo"] = bar; // this can be useful if the key is not constant
or
data[0].foo = "bar"
You could define the columns in your array in a separate object like this:
var cols = { country: new Date(), newSales: Math.random() * 1000, expenses: Math.random() * 5000 };
Then say, data.push(cols);
Now in your button logic, add a new column (or rather property) to the object as follows:
obj.newCol = 'some value';
This will then be automatically reflected in your array