Generating an matrix from a set of arrays - javascript

var set1 = [14, 9, 1, 6, 16],
set2 = [4, 15, 16, 14, 11],
set3 = [16, 10, 2, 3, 8],
set4 = [3, 17, 16, 6, 14],
set5 = [19, 18, 14, 6, 20],
set6 = [6, 15, 8, 7, 2],
set7 = [15, 14, 2, 19, 3],
set8 = [8, 2, 14, 10, 5],
set9 = [11, 6, 8, 10, 18],
set10 = [14, 10, 12, 4, 18],
input = [set1, set2, set3, set4, set5, set6, set7, set8, set9, set10];
// Sort function
function sortFunction(a) {
var len = a.length,
temp, i, j;
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
if (a[j] < a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
// Sorting each sets and finding range of each sets
for (var i = 0; i < len; i++) {
input[i] = sortFunction(input[i]);
minRange.push(Math.min.apply(null, input[i]));
maxRange.push(Math.max.apply(null, input[i]));
}
// Finding the range of input
var minimum = Math.min.apply(null, minRange);
var maximum = Math.max.apply(null, maxRange);
var range = maximum - minimum + 1;
// Matrix table function
var tableArray = [];
function table() {
for (var i = 0; i < len; i++) {
for (var j = 0; j < range; j++) {
if (input[i][j] == j) {
tableArray[i].push(input[i][j]);
} else {
tableArray[i].push(0);
}
}
tableArray.push(tableArray[i]);
}
return tableArray;
}
I am having problem solving this problem: the input is a set of 10 arrays where each array contains 5 different number in range of 1 - 20.
input =[ [14, 9, 1, 6, 16], [4, 15, 16, 14, 11], [16, 10, 2, 3, 8], [3, 17, 16, 6, 14], [19, 18, 14, 6, 20], [6, 15, 8, 7, 2], [15, 14, 2, 19, 3], [8, 2, 14, 10, 5], [11, 6, 8, 10, 18], [14, 10, 12, 4, 18] ]
I would like to generate a 10x20 matrix as output where each row has has 20 numbers with the following pattern:
output = [ [ 1, 0, 0, 0, 0, 6, 0, 0, 9, 0, 0, 0, 0, 14, 0, 16, 0, 0, 0, 0], [ 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 11, 0, 0, 14, 15, 16, 0, 0, 0, 0], [... ], ... ]
Im using JavaScript to solve this...

Create a new output array:
var out = [];
Loop over the input array. For each nested array create a new array in the output array padded with 20 zeros, and then just replace those elements in the output array with the value of the element in the nest input array in the right position. Since you know the size of the nested array, and it's small, its easier just to list each element rather than use an inner loop.
for (var i = 0, l = arr.length; i < l; i++) {
var el = arr[i];
out[i] = Uint8Array(20);
out[i][el[0] - 1] = el[0];
out[i][el[1] - 1] = el[1];
out[i][el[2] - 1] = el[2];
out[i][el[3] - 1] = el[3];
out[i][el[4] - 1] = el[4];
}
DEMO
If your browser doesn't support Uint8Array you can use a separate function to create a padded array:
function padArray() {
var out = [];
for (var i = 0, l = 20; i < l; i++) {
out.push(0);
}
return out;
}
And use:
out[i] = padArray();

You really should have tried it yourself. It's rather easy. Start with an array of 20 zeros, then fill the slots with the values from the array:
function posArray(arr, max) {
var res = [];
for (var i = 0; i < max; i++) res.push(0);
for (var i = 0; i < arr.length; i++) {
var a = arr[i];
if (a > 0 && a <= max) res[a - 1] = a;
}
return res;
}
var output = [];
for (var i = 0; i < input.length; i++) {
output.push(posArray(input[i], 20));
}

Something like this would also work (not tested):
var set1 = [14, 9, 1, 6, 16],
set2 = [4, 15, 16, 14, 11],
set3 = [16, 10, 2, 3, 8],
set4 = [3, 17, 16, 6, 14],
set5 = [19, 18, 14, 6, 20],
set6 = [6, 15, 8, 7, 2],
set7 = [15, 14, 2, 19, 3],
set8 = [8, 2, 14, 10, 5],
set9 = [11, 6, 8, 10, 18],
set10 = [14, 10, 12, 4, 18],
input = [set1, set2, set3, set4, set5, set6, set7, set8, set9, set10];
var output = [];
for (var e=0; e<input.length; e++) {
newRow = [];
for (var i=0;i<20; i++) {
if (input[e].indexOf(i) > -1) {
newRow.push(i);
}
else {
newRow.push(0);
}
}
output.push(newRow);
}
alert(output);

Related

Iterate through array to find values that equal a specified sum

I am trying to write a function that will yield the following output:
// Required sum
console.log([3, 4, 2, 1, 2, 3, 6, 0].elementsTheSumTo(6)) // [ [3, 3], [4, 2], [6, 0] ]
console.log([7, 0, 5, 8, 0, 0, 7, 7].elementsThatSumTo(7)) // [ [7, 0], [0, 7], [0, 7], [0, 7] ]
I've tried with,
Array.prototype.elementsThatSumTo = n => {
let result = [];
for (let i = 0; i < this.length; i++) {
for (let j = 0; j < this.length; j++) {
if (j !== i && (this[i] + this[j] === n) {
result.push([ this[i], this[j] ]);
}
}
}
return result;
}
But that's yielding unexpected behavior. I was also thinking of using reduce, but that didn't seem to work either. Not sure how to figure this one out.
You need to remove the elements from the array when matches are found, which can be done with splice. You also need to use a full-fledged function to access the this, the array instance:
Array.prototype.elementsThatSumTo = function(n) {
const arr = this.slice(); // avoid mutating the input array
const result = [];
while (arr.length) {
const num1 = arr.shift();
const num2Index = arr.findIndex(num => num1 + num === n);
if (num2Index === -1) {
continue;
}
result.push([num1, arr[num2Index]]);
arr.splice(num2Index, 1);
}
return result;
}
console.log([3, 4, 2, 1, 2, 3, 6, 0].elementsThatSumTo(6)) // [ [3, 2], [4, 2], [6, 0] ]
console.log([7, 0, 5, 8, 0, 0, 7, 7].elementsThatSumTo(7)) // [ [7, 0], [0, 7], [0, 7], [0, 7] ]
Keep in mind that mutating built-in prototypes is very bad practice. If possible, consider using a standalone function instead:
const elementsThatSumTo = (arrInit, n) => {
const arr = arrInit.slice(); // avoid mutating the input array
const result = [];
while (arr.length) {
const num1 = arr.shift();
const num2Index = arr.findIndex(num => num1 + num === n);
if (num2Index === -1) {
continue;
}
result.push([num1, arr[num2Index]]);
arr.splice(num2Index, 1);
}
return result;
}
console.log(elementsThatSumTo([3, 4, 2, 1, 2, 3, 6, 0], 6)) // [ [3, 2], [4, 2], [6, 0] ]
console.log(elementsThatSumTo([7, 0, 5, 8, 0, 0, 7, 7], 7)) // [ [7, 0], [0, 7], [0, 7], [0, 7] ]
You could take a Map and store the visited elements and cound the occurence.
Array.prototype.elementsThatSumTo = function (sum) {
var map = new Map,
i, l, v
result = [];
for (i = 0, l = this.length; i < l; i++) {
v = this[i];
if (map.get(v)) {
map.set(v, map.get(v) - 1);
result.push([sum - v, v]);
continue
}
map.set(sum - v, (map.get(sum - v) || 0) + 1);
}
return result;
}
console.log([3, 4, 2, 1, 2, 3, 6, 0].elementsThatSumTo(6)) // [ [3, 2], [4, 2], [6, 0] ]
console.log([7, 0, 5, 8, 0, 0, 7, 7].elementsThatSumTo(7)) // [ [7, 0], [0, 7], [0, 7], [0, 7] ]
Array.prototype.elementsThatSumTo = function(n) {
var result = [],
len = this.length;
for (var i = 0; i < len - 1; i++)
for (var j = i + 1; j < len; j++)
if (this[i] + this[j] == n)
result.push([this[i], this[j]]);
return result;
}
console.log([3, 4, 2, 1, 2, 3, 6, 0].elementsThatSumTo(6))
That's because for every number you are also seeing for the combinations that are already covered. Just change (j=0) to (j=(i+1)) in your code it will work fine and you can also ignore the check(j==i) then.
Array.prototype.elementsThatSumTo = function(n) {
let result = [];
for (let i = 0; i < this.length; i++) {
for (let j = (i+1); j < this.length; j++) {
if (this[i] + this[j] === n) {
result.push([ this[i], this[j] ]);
}
}
}
return result;
}
console.log([3, 4, 2, 1, 2, 3, 6, 0].elementsThatSumTo(6)) // [ [3, 3], [4, 2], [6, 0] ]
console.log([7, 0, 5, 8, 0, 0, 7, 7].elementsThatSumTo(7)) // [ [7, 0], [0, 7], [0, 7], [0, 7] ]

Split javascript array of numbers into ranges

I have an array such as:
[16, 20, 1, 4, 6, 8, 9, 22, 18, 14, 13, 12]
That I would like split into 6 different arrays based on ranges 1-4, 5-8, 9-12, 13-16, 17-20, 21-24.
What is the simplest way to do this with javascript?
You could use an interval for assigning the numbers to a specific slot.
var array = [16, 20, 1, 4, 6, 8, 9, 22, 18, 14, 13, 12],
interval = 4,
result = array.reduce(function (r, a) {
var slot = Math.floor((a - 1) / interval);
(r[slot] = r[slot] || []).push(a);
return r;
}, []);
console.log(result);
The solution using Array.prototype.filter() function:
var list = [16, 20, 1, 4, 6, 8, 9, 22, 18, 14, 13, 12], i
result = [];
// considering ranges `1-4, 5-8, 9-12, 13-16, 17-20, 21-24`
for (i = 1; i < 24; i+= 4) {
result.push(list.filter(function(d){
return ((i+4 > d) && d >= i); // check if the number between lower and upper bound
}));
}
console.log(result);
Simplest answer:
var numbers = [16, 20, 1, 4, 6, 8, 9, 22, 18, 14, 13, 12];
var array1 = []; // range 1-4
var array2 = []; // range 5-8
for(var i=0; i< numbers.length; i++) {
if(numbers[i]>= 1 && numbers[i] <= 4) {
array1[i] = numbers[i]
} else if(numbers[i]>= 5 && numbers[i] <= 8) {
array2[i] = numbers[i]
}
//... continue for remaining ranges
}

how to spirally traverse a matrix recursively - javascript

Please look at this JSFiddle: https://jsfiddle.net/hc2jcx26/
I am trying to spirally traverse a matrix output (any size) so that it prints each element in spiral order to console.log every 2 seconds:
var output = [[0, 1, 2, 3],
[4, 5, 6, 7]];
I am expecting this output:
0
1 //after 2 seconds delay
2 //after 2 seconds delay
3 //etc.
7
6
5
4
But I am not getting this with the code above. The output is all over the place and doesn't even have the right number of elements. I am using recursion to add a delay in my loop after every iteration (through setTimeout), but I do not think I am setting the variables correctly. But when I look at the code, it makes sense to me. What am I missing here?
This is my approach to your problem.
Iterative version
var matrix1 = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
], matrix2 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
], matrix3 = [
[0, 1, 2, 3],
[4, 5, 6, 7]
], matrix4 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
(function (matrix) {
var i,
nRows = matrix.length,
nCols = matrix[0].length,
rowLimit = nRows - 1,
colLimit = nCols - 1,
rounds = 0,
printedElements = 0,
nElements = nRows * nCols,
timeoutLapse = 2000;
function print(val) {
printedElements += 1;
setTimeout(function () {
console.log(val);
}, printedElements * timeoutLapse);
}
do {
for (i = rounds; i <= colLimit - rounds; i += 1) {// from left to right
print(matrix[rounds][i]);
}
for (i = rounds + 1; i <= rowLimit - rounds; i += 1) {// from top to bottom
print(matrix[i][colLimit - rounds]);
}
for (i = colLimit - rounds - 1; i >= rounds; i -= 1) {// from right to left
print(matrix[rowLimit - rounds][i]);
}
for (i = rowLimit - rounds - 1; i >= rounds + 1; i -= 1) {// from bottom to top
print(matrix[i][rounds]);
}
rounds += 1;
} while (printedElements < nElements);
})(matrix4);
Here's the fiddle (you'll have to open the console in order to see the results).
Recursive version
var matrix1 = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
], matrix2 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
], matrix3 = [
[0, 1, 2, 3],
[4, 5, 6, 7]
], matrix4 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
(function(matrix){
var printedElements = 0,
timeoutLapse = 1000;
function print(val) {
printedElements += 1;
setTimeout(function () {
console.log(val);
}, printedElements * timeoutLapse);
}
function printArray(arr) {
for(var i = 0; i < arr.length; i++) {
print(arr[i]);
}
}
// Recursive algorithm, consumes the matrix.
function printMatrix(matrix, direction) {
var dir = direction % 4,
rowLimit = matrix.length - 1,
i;
if (dir === 0) {// from left to right
printArray(matrix.shift());
} else if (dir === 1) {// from top to bottom
for(i = 0; i <= rowLimit; i++) {
print(matrix[i].pop());
}
} else if (dir === 2) {// from right to left
printArray(matrix.pop().reverse());
} else {// from bottom to top
for(i = rowLimit; i >= 0; i--) {
print(matrix[i].shift());
}
}
if (matrix.length) {// Guard
printMatrix(matrix, direction + 1);
}
}
// Initial call.
printMatrix(matrix, 0);
})(matrix4);
I added some examples in order to test it. I just print out elements of the matrix every two seconds following the spiral pattern.
As you can see, the recursive version is more declarative though it empties the matrix completely. Here's the fiddle
try
var output = [[0, 1, 2, 3],
[4, 5, 6, 7]];
var columns = output[0].length;
var row;
function spiralOrderRecursive (matrix, rowIndex) {
rowIndex = rowIndex || 0;
if (matrix.length) {
row = rowIndex % 2 ? matrix[0].reverse() : matrix[0];
row.forEach(function (item, index) {
setTimeout(function () {
console.log(item);
}, (rowIndex * columns + index) * 2000);
});
spiralOrderRecursive (matrix.slice(1), ++rowIndex);
}
}
spiralOrderRecursive(output);
also NON RECURSIVE
var output = [[0, 1, 2, 3, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]];;
var columns = output[0].length;
function spiralOrder(output) {
output.forEach(function (row, rowIndex) {
row = rowIndex % 2 ? row.reverse() : row;
row.forEach(function (item, index) {
setTimeout(function () {
console.log(item);
}, (rowIndex * columns + index) * 2000);
});
});
}
spiralOrder(output);
Pseudo-code for what you want to do
spiral_print(matrix)
{
If the matrix has one row
print the row and go to new line
else if the matrix has two rows
print the first row and go to new line
sleep(2)
print the second row in reverse order and go to new line
else
print the first row and go to new line
sleep(2)
print the second row in reverse order and go to new line
sleep(2)
spiral_print(matrix.slice(2))
}
Using JS
UPDATE: Leveraging #acontell's print function
function spiral(matrix) {
if (matrix.length <= 0) {
return
}
for (var i = 0; i < matrix[0].length; i++) {
print(matrix[0][i])
}
//Get last col
for (var i = 1; i < matrix.length; i++) {
print(matrix[i][matrix[0].length - 1])
}
//Get last row
for (var i = matrix[0].length - 2; i >= 0; i--) {
print(matrix[matrix.length-1][i])
}
//Get first column
for (var i = matrix.length - 2; i > 0; i--) {
print(matrix[i][0])
}
matrix = matrix.slice(1, -1)
for (var i = 0; i < matrix.length; i++) {
matrix[i] = matrix[i].slice(1, -1)
}
spiral(matrix)
}
function print(val) {
printedElements += 1;
setTimeout(function () {
console.log(val);
}, printedElements * 2000);
}
var printedElements = 0
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
matrix1 = [[1, 2, 3, 4, 31],
[5, 6, 7, 8, 32],
[9, 10, 11, 12, 33],
[13, 14, 15, 16, 34],
[35, 36, 37, 38, 39]]
spiral(matrix1)
Using Python
Note: It's much easier with numpy
def spiral(matrix):
if len(matrix) <= 0:
return
for v in matrix[0]:
print(v)
last_col = [matrix[i][len(matrix[0]) - 1] for i in range(1,len(matrix))]
for v in last_col:
print(v)
last_row = reversed(matrix[len(matrix)-1][0:-1])
for v in last_row:
print(v)
first_col = [matrix[i][0] for i in range (1, len(matrix) - 1)]
first_col = reversed(first_col)
for v in first_col:
print(v)
matrix = matrix[1:len(matrix) - 1]
matrix = [m[1:len(matrix[0]) - 1] for m in matrix]
spiral(matrix)
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
matrix1 = [[1, 2, 3, 4, 31],
[5, 6, 7, 8, 32],
[9, 10, 11, 12, 33],
[13, 14, 15, 16, 34],
[35, 36, 37, 38, 39]]
spiral(matrix1)
UPDATE: With numpy
import numpy as np
def spiral(matrix):
if len(matrix) <= 0:
return
for v in matrix[0]:
print(v)
last_col = matrix[1:, -1]
for v in last_col:
print(v)
last_row = reversed(matrix[-1, :-1])
for v in last_row:
print(v)
first_col = reversed(matrix[1:-1, 0])
for v in first_col:
print(v)
matrix = matrix[1:-1, 1:-1]
spiral(matrix)
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
matrix1 = [[1, 2, 3, 4, 31],
[5, 6, 7, 8, 32],
[9, 10, 11, 12, 33],
[13, 14, 15, 16, 34],
[35, 36, 37, 38, 39]]
matrix2 = np.array([[1, 2, 3, 4, 31],
[5, 6, 7, 8, 32],
[9, 10, 11, 12, 33],
[13, 14, 15, 16, 34],
[35, 36, 37, 38, 39]])
spiral(matrix2)

How to iterate only on part of 2D array in JavaScript?

I have 2D array
var arr = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]];
Now we are dividing this into smaller parts like 2x2. How to iterate only on part (block) of this array e.g: items with indexes arr[2][4], arr[2][5], arr[3][4], arr[3][5]?
Edit:
Seems question is not easy understandable. I want to iterate over blocks.
var blocks = 9;
var output = '';
for( var block = 0; block < blocks; block++) {
// actual iteration over array
for(var i = ... ) {
for(var j = ... ) {
output += arr[i][j] + ' ';
}
}
console.log(output);
output = '';
}
Expected output would be:
0 1 6 7
2 3 8 9
4 5 10 11
12 13 18 19
14 15 20 21
16 17 22 23
24 25 30 31
26 27 32 33
28 29 34 35
You need to nest for loops.
var arr = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]];
function loopThrough() {
for(int i = 0; i < arr.length; i++) {
// this will give you arr[0], arr[1], etc.
for(int j = 0; j < arr.length; j++) {
// this will give you arr[0][0], arr[0][1], etc.
console.log(arr[i][j]);
}
}
}
loopThrough();
If you want it to loop over only the last 2, then set j] = arr[i].length-2
<script type="text/javascript">
var arr = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]];
function loopThrough() {
for(var i = 0; i < arr.length; i++) {
// this will give you arr[0], arr[1], etc.
for(var j = arr[i].length-2; j < arr[i].length; j++) {
// this will give you arr[0][0], arr[0][1], etc.
console.log(arr[i][j]);
}
}
}
loopThrough();
</script>
function subblock_array(arr, startX, endX, startY, endY) {
var subArr = [];
for (var ii=startX; ii<endX; ii++) {
subArrY = [];
for (var jj=startY; jj<endY; jj++) {
subArrY.push(arr[ii][jj]);
}
subArr.push(subArrY);
}
return subArr;
}
subblock_array(arr, 2, 4, 4, 6)
You want to access those indexes in order :
arr[0][0] arr[0][1] arr[1][0] arr[1][1]
arr[0][2] arr[0][3] arr[1][2] arr[1][3]
[...]
arr[0][y-1] arr[0][y] arr[1][y-1] arr[1][y] // first two lines processed
arr[2][0] arr[2][1] arr[3][0] arr[3][1] // we continue with the next two lines
[...]
arr[2][y-1] arr[2][y] arr[3][y-1] arr[3][y]
[...]
arr[x-1][0] arr[x-1][1] arr[x][0] arr[x][1]
[...]
arr[x-1][y-1] arr[x-1][y] arr[x][y-1] arr[x][y]
As you can see we have two levels of iteration : a first one iterates over the lines, skipping every other one, and a second one iterates over the columns, skipping every other one.
The following code should implement this :
for (var x=0; x < arr.length; x+=2) {
for (var y=0; y < arr[x].length; y+=2) {
console.log(arr[x][y] + " " + arr[x][y+1] + " " + arr[x+1][y] + " " + arr[x+1][y+1]);
}
}
You will notice that we increment the iterations variables by two each loop, since the inner block consumes two columns and lines at once.
You would need a loop with an offset inner loop to iterate over the last two positions. Something like this would work, you can see the results on this js fiddle: https://jsfiddle.net/0wfysb38/3/
var arr = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]];
for(i = 0; i < arr.length - 1; i=i+2) {
for(j = 0; j < arr[i].length - 1; j=j+2) {
strOut = '';
strOut = strOut + ', ' +arr[i][j];
strOut = strOut + ', ' +arr[i][j+1];
strOut = strOut + ', ' +arr[i+1][j];
strOut = strOut + ', ' +arr[i+1][j+1];
strOut = strOut.substring(1,strOut.length)
console.log(strOut);
}
}
Edit: Updated to code to produce results matching the expected output. The key is to iterate the loops by 2, and seek the next position in the array by offsetting. This is not very flexible, and the array this loops over should be structured appropriately.

Create an array of first element of each array

I have an array in maxtrix form like this
var costs = [
[4, 6, 8, 8],
[6, 8, 6, 7],
[5, 7, 6, 8],
];
how do i transform it to this
[[4,6,5], [6,8,7], [8,6,5], [8,7,5]]
This is what am trying
var cols = [];
for(var i = 0; i<costs.length; i++)
{
cols.push(costs[i][0]);
}
return col;
And this gives me [4,6,5]. I know am missing something, any help will be great. Thanks
You need another for loop inside the first one:
var costs = [
[4, 6, 8, 8],
[6, 8, 6, 7],
[5, 7, 6, 8],
];
alert("Original: " + JSON.stringify(costs));
// Prepare array...
var cols = new Array(costs[0].length);
for(var i = 0; i < costs[0].length; i++) {
cols[i] = new Array(costs.length);
}
// Assign values...
for(var i = 0; i < costs.length; i++) {
for(var k = 0; k < costs[i].length; k++) {
cols[k][i] = costs[i][k];
}
}
alert("New: " + JSON.stringify(cols));

Categories

Resources