Using a complex array (for the use case of tabular data displayed in columns and rows) lets say I have some values:
var values = [
[234, 386, 21, 38],
[-23, 58, 106, 0],
[45, -48, 506, 23],
[109, 168, 42, 111]
];
What would be the best way to return a matching array that would rank the values against their column in the correct (maintained) order? The ranked order would be highest to lowest in this case.
For example, an end result array of:
[
[1, 1, 4, 2],
[4, 3, 2, 4],
[3, 4, 1, 3],
[2, 2, 3, 1]
];
Note how the result is sorted vertically by column.
I would want to use this with a large data set, so any guidance/tips for quickest performance is my aim here.
--
For context: My first attempt was to map the original array with the index values, but I was unsure where to go from there:
var singleColumn = [234, -23, 45, 109];
for (var i = 0; i < singleColumn.length; i++) {
singleColumn[i] = [singleColumn[i], i];
}
Essentially the trick will be retaining original indices after the sort. I've iterated them into a data structure first, sorted it, and then rebuilt the 2-dimensional array structure from the result.
I haven't done any checking to ensure that the input is all well-formed, the assumption is all rows are the same width as the first row.
An optimization that could probably be done would be transforming the raw values into the data-structure during the sort, which would eliminate a pass of the array. I don't see an easy way to do that without losing some of the conciseness and readability, and it would be a pretty small gain.
var values = [
[234, 386, 21, 38],
[-23, 58, 106, 0],
[45, -48, 506, 23],
[109, 168, 42, 111]
];
function buildRanking(arr) {
var result = [];
for(var col = 0; col < arr[0].length; col++) {
//everything inside this for loop is per column
//first use map function to turn the column into an array of objects
//each holding the value and the current index. [{value: 234, index: 1}, etc..]
var sortableStructure = values.map(function(val, i) {
return { value : val[col], index : i };
});
//Sort function to sort my sortableStructure in place on the values
sortableStructure.sort(function(a, b) {
return b.value - a.value;
});
//now iterate over the sortable strucutre
for(var i = 0; i < sortableStructure.length; i++) {
//this ugly bit just makes sure arrays are initialized for each row as needed
if(typeof result[sortableStructure[i].index] === 'undefined')
result[sortableStructure[i].index] = [];
//for the current item in the sortableStructure, get the index
//access the result element corresponding to that index
//(the original position of this sorted value) and push in
//the current index (sort order) + 1 (to switch from zero-based to one-based)
result[sortableStructure[i].index].push(i + 1);
}
}
return result;
}
//To provide visible output.
document.write(JSON.stringify(buildRanking(values)).split('],[').join('],<br/>['));
First pass at this and highly unoptimized but here's a loose implementation where I do it step by step.
function sort(rows) {
var columns = [];
/* Invert rows and columns */
for (var i = 0, row; i < rows.length; i++) {
row = rows[i];
for (var j = 0, col; j < row.length; j++) {
col = rows[i][j];
columns[j] = columns[j] || [];
columns[j][i] = col;
}
}
/* Sort by descending order, returning index */
var sortedColumns = columns.slice(0).map(function(column, i) {
return column.slice(0).sort(function(a, b) {
return b - a;
}).map(function(value, j, sortedColumn) {
return sortedColumn.indexOf(column[j]) + 1;
});
});
/* Invert rows and columns back again */
var sortedRows = [];
for (var i = 0, row; i < sortedColumns.length; i++) {
row = sortedColumns[i];
for (var j = 0, col; j < row.length; j++) {
col = sortedColumns[i][j];
sortedRows[j] = sortedRows[j] || [];
sortedRows[j][i] = col;
}
}
return sortedRows;
}
var values = [
[234, 386, 21, 38],
[-23, 58, 106, 0],
[45, -48, 506, 23],
[109, 168, 42, 111]
];
var expected = [
[1, 1, 4, 2],
[4, 3, 2, 4],
[3, 4, 1, 3],
[2, 2, 3, 1]
];
var sorted = sort(values);
console.log(sorted.toString() === expected.toString()); // true
JSFiddle demo: https://jsfiddle.net/7ek6pz63/2/
Assuming the columns habe to be sorted descending and the result is based on one, then this should work.
var values = [
[234, 386, 21, 38],
[-23, 58, 106, 0],
[45, -48, 506, 23],
[109, 168, 42, 111]
];
function getX(array) {
var length = array[0].length,
result = Array.apply(null, { length: array.length }).map(function () { return []; }),
temp, i;
for (i = 0; i < length; i++) {
temp = [];
array.forEach(function (a, j) {
temp.push({ v: a[i], i: j });
});
temp.sort(function (a, b) {
return b.v - a.v;
}).forEach(function (a, j) {
result[a.i][i] = j + 1;
});
}
return result;
}
document.write('<pre>' + JSON.stringify(getX(values), 0, 4) + '</pre>');
Related
I have an array of strings that, after a lot of effort, I have managed to turn into several arrays with a loop. So right now, the loop is giving me something like:
[4,5,6,7,8]
[4,5,6,7,8],[1,2,3,4,5]
[4,5,6,7,8],[1,2,3,4,5],[22,33,44,55,66]
If I place the return lower in the code, I get:
[[4,5,6,7,8],[1,2,3,4,5],[22,33,44,55,66]]
What I need is the vertical sum of these arrays, so in this case it'd be:
[27,40,53,66,80]
So far, I'm usign '.push'. Also, console.log gives me this answer but return results in 'undefined'. Any help with these two things would be welcome!
----UPDATE----
As someone here suggested, I tried this but it doesn't work entirely:
array=[ [ 1, 2, 4 ], [ 4, 1, 5 ], [ 0, 5, 2 ] ];
let adding=0
const result=[]
for (let i = 0; i < array[0].length; ++i) {
for (let j = 0; j < array.length; ++j) {
adding += array[j][i];
}
result.push(adding);}
console.log(result)
```
The ouput is: [ 5, 13, 24 ] instead of [5,8,11]
1) You can easily achieve the result using map and reduce
const arr = [
[4, 5, 6, 7, 8],
[1, 2, 3, 4, 5],
[22, 33, 44, 55, 66],
];
const result = arr[0].map((_, i) => arr.reduce((acc, curr) => acc + curr[i], 0));
console.log(result)
2) Using simple for loops
const arr = [
[4, 5, 6, 7, 8],
[1, 2, 3, 4, 5],
[22, 33, 44, 55, 66],
];
const result = [];
for (let i = 0; i < arr[0].length; ++i) {
let sum = 0;
for (let j = 0; j < arr.length; ++j) {
sum += arr[j][i];
}
result.push(sum);
}
console.log(result);
I have n (but for now, let say just two) of one dimensional arrays like this image of my console :
And I want to merge these two arrays by the corresponding key and put it into two dimensional array :
The result is something like :
[["1 279 226,08" , "127"],[null , null],["-188 033,77", "154"],..... so on ......]
And the list of one dimensional array is dynamic, it could be more than 2 arrays.
So example if I have 3 arrays, then my two dimensional array would look like :
[ ["1 279 226,08" , "127" , "blabla"], [null , null , "blabla"], ["-188 033,77", "154", "blabla"], ..... so on ......]
Any ideas of implementing it would be appreciate.
You could transpose the array with a nested loop and switch the indices for assigning the values.
var array = [["1 279 226,08", null, "-188 033,77"], ["127", null, "154"], ["blabla", "blabla", "blabla"]],
result = array.reduce(function (r, a, i) {
a.forEach(function (b, j) {
r[j] = r[j] || [];
r[j][i] = b;
});
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Since all of your arrays have the same size, you can loop just on the lenght of the first array, i.e. a.
Then we pass to appendArrays the single value of a , b, ..., and we return the an array to push into merged
var a = ["123", null, "ciao"]
var b = ["321", 1, "pippo"]
var c = ["111", 5, "co"]
var merged = []
for (i = 0; i < a.length; i++) {
merged.push(appendArrays(a[i], b[i], c[i]));
}
console.log(merged);
function appendArrays() {
var temp = []
for (var i = 0; i < arguments.length; i++) {
temp.push(arguments[i]);
}
return temp;
}
This should do what you want.
var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = [6, 5, 4, 3, 2, 1];
var arr3 = [7, 8, 9, 10, 11, 5];
var arr4 = [12, 34, 55, 77, 22, 426];
var arrCollection = [arr1, arr2, arr3, arr4];
// if array sizes are variable.
// if not max = arrCollection[0].length will do
var max = Math.max.apply(Math, arrCollection.map(function(a) {
return a.length;
}));
var arrFinal = [];
for (let i = 0; i < max; i++) {
var arr = [];
arrCollection.forEach(function(a) {
arr.push(a[i]);
});
arrFinal.push(arr);
}
console.log(arrFinal);
You can create this with two forEach() loops.
var arr1 = [[1, 2], [3, 4], [5, 6]];
var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 12, 14]];
let merge = function(arr) {
var result = [];
arr.forEach(function(e, i) {
e.forEach(function(a, j) {
if (!result[j]) result[j] = [a];
else result[j].push(a)
})
});
return result;
}
console.log(JSON.stringify(merge(arr1)))
console.log(JSON.stringify(merge(arr2)))
This code sorts the array after inserting another element and returns the index of the inserted element in the sorted array (the first position or lowest possible index needs to be returned).
CODE:
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var sortedarr = sort(combinelists(arr, num).sort());
var pos = [];
for (i = 0; i < sortedarr.length; i++) {
if (sortedarr[i] == num) {
pos.push(i);
}
}
return pos[0];
}
function combinelists(arr1, arr2) {
var newarr = [];
newarr.push(arr2);
for (i = 0; i < arr1.length; i++) {
newarr.push(arr1[i]);
}
return newarr;
}
function sort(arr) {
if (arr.length < 2) {
return arr;
} else {
var l = arr.length / 2;
var leftarr = arr.slice(0, l);
var rightarr = arr.slice(l);
return combine(sort(leftarr), sort(rightarr));
}
}
function combine(array, another_array) {
var result = [];
while (array.length && another_array.length) {
if (array[0].age <= another_array[0].age) {
result.push(array.shift());
} else {
result.push(another_array.shift());
}
}
while (array.length)
result.push(array.shift());
while (another_array.length)
result.push(another_array.shift());
return result;
}
console.log(getIndexToIns([2, 20, 10], 19));
console.log(getIndexToIns([2, 5, 10], 15));
But it doesn't seem to work for all inputs:
It works for the following tests:
[10, 20, 30, 40, 50], 30
[40, 60], 50
[2, 20, 10], 19
But it doesn't work for these:
[2, 5, 10], 15
[5, 3, 20, 3], 5
[3, 10, 5], 3
[10, 20, 30, 40, 50], 35
What's broken?
You use Array#sort() without compareFunction, that means you get a result which every element is treated as string and not as number. That results, probably, to the wrong index.
var sortedarr = sort(combinelists(arr,num).sort());
// ^^^^^^
You coud use a callback like
var sortedarr = sort(combinelists(arr,num).sort(function (a, b) { return a - b; }));
for sorting by numbers.
function largestOfFour(arr) {
var max = 0;
var newArr = [];
for (var i = 0; i < arr.length; i++) {
for (var j = i; j < arr.length; j++) {
max = Math.max(max, arr[i][j]);
}
newArr.push(max);
}
return newArr;
}
Here is my code. It works for me but I want to know is there any other sort way to do this?
Try this:
function largestOfFour(arr) {
return arr.map(function(item){
return Math.max.apply(null, item);
});
}
You may use the spread operator as:
var data = [
[1, 2, 3, 4],
[10, 20, 200, 31],
[21, 3, 444, 133],
[0, 0, 90, 1]
];
const max = Math.max(...data.map(inner => Math.max(...inner)));
console.log(max);
You can try something like this:
Idea, Math.max takes n arguments and gives you the max value. Using .apply you can pass parameters as array. Combining both will give you max value in an array.
Apply
var data = [
[1, 2, 3, 4],
[10, 20, 200, 31],
[21, 3, 444, 133],
[0, 0, 90, 1]
];
var max_arr = data.map(function(a) {
return Math.max.apply(this, a);
});
console.log(max_arr)
Sort + slice + pop
var data = [
[1, 2, 3, 4],
[10, 20, 200, 31],
[21, 3, 444, 133],
[0, 0, 90, 1]
];
var max_arr = data.map(function(a) {
return a.sort().slice(-1).pop()
});
console.log(max_arr)
I have a function that picks all elements from a 2-dimensional array by its rows and returns a 1-dimensional array.
The array has a variable amount of columns and rows.
Example:
let arr = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
];
Returns:
[1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12]
The function i came up with:
convertList = (list) => {
let result = [];
let listTotalEntries = R.sum(R.map(R.length)(list));
let mod = R.modulo(R.__, list.length);
let counterRow = -1;
for (let i = 0; i < listTotalEntries; i++) {
if (mod(i) === 0) {
counterRow++;
}
if (list[mod(i)][counterRow]) {
result.push(list[mod(i)][counterRow]);
console.log(list[mod(i)][counterRow]);
}
}
console.log(result);
return result;
};
Question: This function works only with square matrices - how can i make it work with a variable length of the contained arrays?
Example:
let arr = [
[1, 2],
[],
[9, 10, 11, 12]
];
Should return:
[1, 9, 2, 10, 11, 12]
Thanks for your help!
Muff
You had a ramda.js tag in here. With Ramda, it's pretty simple, since there are two functions that will help:
const convertList = compose(flatten, transpose);
convertList(arr); //=> [1, 9, 2, 10, 11, 12]
transpose flips a matrix over its main diagonal, that is, changing rows to columns and vice versa. flatten turns a list of lists into a plain list. So composeing like this essentially creates the equivalent of list => flatten(transpose(list)).
You can see this in action on the Ramda REPL.
I suggest to go step-by-step through the arrays
var arr1 = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],
arr2 = [[1, 2], [], [9, 10, 11, 12]];
function single(array) {
var r = [],
max = Math.max.apply(null, array.map(function (a) { return a.length; })),
i = 0, j,
l = array.length;
while (i < max) {
for (j = 0; j < l ; j++) {
i in array[j] && r.push(array[j][i]);
}
i++;
}
return r;
}
document.write('<pre>' + JSON.stringify(single(arr1), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(single(arr2), 0, 4) + '</pre>');
Did you try this simple one?
var singleDimensionArr = arr.reduce(function(prev,current){return prev.concat(current)});
For example
[
[1, 2],
[],
[9, 10, 11, 12]
].reduce(function(prev,current){return prev.concat(current)});
outputs [1, 2, 9, 10, 11, 12]
Edit:
Based on the inputs from OP below, since the concatenation needs to happen column wise
var max = Math.max.apply(null, arr.map(function (a) { return a.length; }));
var finalArr = []; for( var i = 0; i < max; i++)
{
for( var j = 0; j < arr.length; j++)
{
arr[j][i] ? finalArr.push(arr[j][i]) : "";
}
}
console.log(arr);
This example makes a big sparse array putting each item where it would belong if the array were square. Then it filters out null values which occur where no input item was present.
let arr = [
[1, 2],
[],
[9, 10, 11, 12]
];
var out = arr.reduce(function(o,n,i,a) {
for (var j=0;j<n.length;j++){
o[a.length * j + i] = n[j];
}
return o;
},[]).filter(function(n) {
return n !== null;
});
alert(JSON.stringify(out));