This question already has answers here:
Swap rows with columns (transposition) of a matrix in javascript [duplicate]
(5 answers)
Javascript equivalent of Python's zip function
(24 answers)
Closed 8 years ago.
how to dynamically convert this type of array:
[
[a,b,c],
[d,e,f],
]
into
[
[a,d],
[b,e],
[c,f],
]
the length of the first array is not always the same size.
tried the following
for (var i = 0; i < multi.length; i++) { // 2
for (var j = 0; j < multi[i].length; j++) { // 3
multi2[j].push(multi[j][i])
}
}
it does not work
Two issues:
Initialize your multi2 subarray for i.
You have your i and j mixed up in the inner loop.
Here's a fiddle
var multi = [
["a","b","c"],
["d","e","f"],
["g","h","i"],
]
var multi2 = [];
for (var i = 0; i < multi.length; i++) { // 3
for (var j = 0; j < multi[i].length; j++) { // 3
multi2[j] = multi2[j]||[]; // initialize subarray if necessary
multi2[j].push(multi[i][j])
}
}
Related
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Returning only certain properties from an array of objects in Javascript [duplicate]
(5 answers)
Javascript Array of objects get single value [duplicate]
(3 answers)
Construct an array of elements from an array of objects? [duplicate]
(2 answers)
Closed 4 years ago.
How to save javascript data from a loop to an array?
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
}
Insert the data in the array using push
var arr=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
arr.push(h);
}
var data = [];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
data.push(h)
}
//OR
var data = jsonData.Data.Positions.map(item => item.Oid);
Your variable jsonData.Data.Positions is probably already an array.
Use .push() method to add values to array.
var h=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
h.push(jsonData.Data.Positions[i].Oid);
}
console.log(h);
You can do it within a loop:
var array = []
for (i = 0; i < jsonData.Data.Positions.length; i++) {
array.push(jsonData.Data.Positions[i].Oid);
}
Or in a more functional-way:
var array = jsonData.Data.Positions.map(p => p.Oid)
map instead of for loop
var h = jsonData.Data.Positions.map(function (x) { return x.0id });
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 6 years ago.
I tried something like:
var diff = $(array1).not(array2).get();
console.log(diff); // store the difference
And replace .not() with .is() but that didn't work..which generated an error. It only stored the difference, but I want only the same values stored in a new array. How do I do that in jQuery regardless if it's length size in both arrays?
var array1 = ['a','b','c','d','e','f','g'];
var array2 = ['c', 'b'];
var sameValArr = [];
// TODO:
// 1. compare the two arrays if there's any matching values in both
// 2. if yes, store the matching value in a new array; if no, do nothing
// 3. check the new array if it isn't empty
// 4. if empty, hide the video; if not empty do nothing (show video)
for(var i = 0; i < array1.length; i++)
{
for(var j = 0; j < array2.length; j++)
{
if(array1.indexOf([i]) === array2.indexOf([j]))
{
sameValArr.push(array1[i]);
console.log("sameValArr: ", sameValArr);
}
}
}
The answer provided to this question using indexOf() method didn't work.
for(var i=0; i< array1.length; i++)
{
for(var j=0; j< array2.length; j++)
{
if(array1[i] === array2[j])
sameValArr.push(array1[i]);
}
{
This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Closed 7 years ago.
I have an array that I want to loop with a for.
for(var j = 0; j < outPutData.length; j++)
{
if(outPutData[j].pregunta1==1) { }
}
pregunta1, pregunta2, etc. are some of the variables.
If I print the value of outPutData[j].pregunta1 with an alert() it shows the correct value, but when I try:
for(var j = 0; j < outPutData.length; j++)
{
if(outPutData[j].pregunta+j==1) { }
OR
if(outPutData[j].pregunta+""+j==2) { }
}
an error is shown. Why?
What I expect to have is
outPutData[j].pregunta1
outPutData[j].pregunta2
outPutData[j].pregunta3
... (more till 200)...
outPutData[j].pregunta200
What am I doing wrong?
Thanks!
Try this:
outPutData[j]['pregunta' + j]
you can access properties of object by string using []:
var obj = {prop: "value"}
var a = obj.prop;
// is the same as
var b = obj['prop'];
This question already has answers here:
Transposing a 2D-array in JavaScript
(25 answers)
Closed 8 years ago.
If I have a large multidimensional array (matrix), how can rearange the elements so that I have "y" values as the new "x" values in the array?
Hard to explain so let me give you an example.
I want the below array.
[
[[0][1][2][3]],
[[4][5][6][7]],
[[8][9][10][11]],
[[12][13][14][15]],
]
to be transformed into the below array
[
[[0][4][8][12]],
[[1][5][9][13]],
[[2][6][10][14]],
[[3][7][11][15]],
]
for (var i = 0; i < tablesOfData.length; i++) {
for (var j = 0; j < tablesOfData[i].length; j++) {
//Transform the array
}
}
All rows needs to have the same number of columns.
If number of rows is the same as number of columns (like in your example), the following should work. If they are not the same then you need to create a n
for (var i = 0; < i tablesOfData.length; i++) {
for (var j = i; j < tablesOfData.length; j++) {
var temp = tablesOfData[i][j];
tablesOfData[i][j] = tablesOfData[j][i];
tablesOfData[j][i] = tablesOfData[i][j];
}
}
Otherwise you need to create a new table and add the values to that table, like this:
var newTable = new int[tablesOfData[i].length]();
for (var i = 0; < i tablesOfData.length; i++) {
for (var j = 0; j < tablesOfData.length; j++) {
if (i == 0)
newTable[j] = new int[tablesOfData.length]();
newTable[j][i] = tablesOfData[i][j];
}
}
I wrote the code in Notepad so it might not be the correct syntax to run on it's own but the logic should be right.
This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Closed 9 years ago.
How do you create a multi dimensional array in Javascript using a for loop ?
var test = [];
for(var i = 0; i < 100; i++){
test.push([i, "lol"]);
}
var sDataArray = MultiDimensionalArray(2, 2);
function MultiDimensionalArray(iRows, iCols) {
var i;
var j;
var table = new Array(iRows);
for (i = 0; i < iRows; i++) {
table[i] = new Array(iCols);
for (j = 0; j < iCols; j++) {
table[i][j] = "";
}
}
return (table);
}
var arr = [];
for(var i = 0;i<100;++i){
arr[i] = [];
for(var j = 0; j < 100; ++j){
arr[i][j] = i*j;
}
}
Its an array of arrays.
var arr = [
[0,1,2],
[3,4,5],
[
['a','b','c']
]
];