Create an array of first element of each array - javascript

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));

Related

Cant copy elements of 2D array into another array using for loop [duplicate]

This question already has answers here:
Create copy of multi-dimensional array, not reference - JavaScript
(4 answers)
Closed 1 year ago.
What's wrong with this code?
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = []; // or arr = [[]];
for (let i = 0; i < matrix1.length; i++) {
for (let j = 0; j < matrix1[i].length; j++) {
arr[i][j] = matrix1[i][j];
}
}
console.log(arr);
Error is:
Cannot set property '0' of undefined
This is when I try to assign the value of an element of matrix1 to the new array.
for loop works for the single dimensional array.
try this
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = []; // or arr = [[]];
for (let i = 0; i < matrix1.length; i++) {
for (let j = 0; j < matrix1[i].length; j++) {
if(!arr[i])
arr[i] = []
arr[i][j] = matrix1[i][j];
}
}
if you want copy a 2d array without for loop try this one:
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = JSON.parse(JSON.stringify(matrix1))
You need to create a new sub array in the outer loop so that arr[i] is an array and not undefined
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = [];
for (let i = 0; i < matrix1.length; i++) {
// push a new sub array to be populated in next loop
arr.push([]);
for (let j = 0; j < matrix1[i].length; j++) {
arr[i][j] = matrix1[i][j];
}
}
console.log(arr);
The problem is the line arr[i][j] = matrix1[i][j];, since arr[i][j] is undefined at this point.
The correct way of adding elements to an array is using the .push() function:
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = [];
for (let i = 0; i < matrix1.length; i++) {
arr.push([]);
for (let j = 0; j < matrix1[i].length; j++) {
arr[i].push(matrix1[i][j]);
}
}
console.log(arr);
Also note that using JSON might achieve the same task in a simpler way:
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = JSON.parse(JSON.stringify(matrix1))
console.log(arr)

How to use for loop to remove items from array of array without using js methods?

I'm using for loop to remove only 3 from array of array?
I have tried few conditional statements but it's not working for me.
let myArray = [
[1, 2, 3],
[5, 4, 3],
[3, 6, 7],
[3, 8, 9]
];
let newArray = [];
for (let i = 0; i < myArray.length; i++) {
if (myArray[i] !== 3) {
newArray.push(myArray[i]);
console.log(newArray);
}
}
I am expect the output 1,2,4,5,6,7,8,9, but actual output is with 3.
let myArray = [
[1, 2, 3],
[5, 4, 3],
[3, 6, 7],
[3, 8, 9]
];
let newArray = [];
for (let i = 0; i < myArray.length; i++) {
for(let j=0; j< myArray[i].length; j++) {
if (myArray[i][j] != 3) {
newArray.push(myArray[i][j]);
}
}
}
console.log(newArray);
myArray[i] is an array, you have to loop through the items of myArray[i] as well.
let myArray = [ [1, 2, 3], [5, 4, 3], [3, 6, 7], [3, 8, 9]];
let newArray = [];
for(let i=0; i<myArray.length; i++) {
for(let j=0; j<myArray[i].length; j++) {
if (myArray[i][j] != 3) {
newArray.push(myArray[i][j]);
}
}
}
console.log(newArray);
You have a two dimentional array but you are treating it like is one dimentional array.
Try the following:
let myArray = [
[1, 2, 3],
[5, 4, 3],
[3, 6, 7],
[3, 8, 9]
];
let newArray = [];
for (let i = 0; i < myArray.length; i++) {
newArray.push([]);
for (let j = 0; j < myArray[i].length; j++) {
if (myArray[i][j] != 3) {
newArray[i].push(myArray[i][j]);
}
}
}
console.log(newArray);
The problem is with your flow.
Here is what your code should look like:
var newArray = [];
var temp = [];
var myArray = [
[1, 2, 3],
[5, 4, 3],
[3, 6, 7],
[3, 8, 9]
];
for (var i = 0; i < myArray.length; i++) {
temp = [];
for( var j = 0; j < myArray[i].length; j++){
if (myArray[i][j] != 3) {
temp[j]= myArray[i][j];
}
}
newArray[i] = temp;
}
console.log(newArray);
This is a single loop approach by using a dynamic check of the length, depending on the actual loop purpose.
You could assign all values and take for the nested array a new index k to assign to the right index. This index is only incremented on a valid value.
var myArray = [[1, 2, 3], [5, 4, 3], [3, 6, 7], [3, 8, 9]],
newArray = [],
i, j, k;
for (i = 0; i < (myArray[j] || myArray).length; i++) {
if (j === undefined) {
j = i;
i = 0;
k = 0;
newArray[j] = [];
}
if (myArray[j][i] !== 3) {
newArray[j][k++] = myArray[j][i];
}
if (i + 1 >= myArray[j].length) {
i = j;
j = undefined;
}
}
console.log(newArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Javascript Basic Logic - print each three and four cycle from 1 to n

I want to see the output like
[0, 1, 2]
[3, 4, 5, 6]
[7, 8, 9]
[10, 11, 12, 13]
[14, 15, 16]
...
recognize every three(a cycle) and four(b cycle) to do something.
I have only figure out some bad way I think like following:
var arr = [];
function a(n) {
var eachCycle = 7;
var aCycle = 3;
var bCycle = 0;
arr.push(0);
for (var i = 1; i < n; i += 1) {
if (i % eachCycle === aCycle || i % eachCycle === bCycle) {
if(i % eachCycle === aCycle) {
// print three column
} else if(i % eachCycle === bCycle) {
// print four column
}
console.log(arr);
arr.length = 0;
}
arr.push(i)
}
}
is there any good idea to improve this function for the output !?
Thanks
How about this:
function a(n)
{
// keep track of all the cycles
var total = [];
// hold values for the current cycle
var temp = [];
// cycle sizes
var cycleSizes = [3, 4];
// index of the current cycle
var currentCycleIndex = 0;
// iterate through numbers
for(var i = 0; i < n; ++i)
{
// push the value into the temp
temp.push(i);
// if the length of the temp array is the length we want for the current cycle then
if(temp.length == cycleSizes[currentCycleIndex])
{
// save the cycle data
total.push(temp);
// reset the cycle
temp = [];
// change the cycle
currentCycleIndex = currentCycleIndex ^ 1;
}
}
return total;
}
a(9);
[
[0, 1, 2],
[3, 4, 5, 6],
[7, 8]
];
a(17);
[
[0, 1, 2],
[3, 4, 5, 6],
[7, 8, 9],
[10, 11, 12, 13],
[14, 15, 16],
];

Iterating over rows of 2-dimensional array containing arrays of different length

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));

Generating an matrix from a set of arrays

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);

Categories

Resources