array of arrays in js function - javascript

I was doing some tasks with this function, but for some reason it does not return values as I think it should.
function intersection(arrays) {
return arrays;
}
console.log(intersection([5, 10, 15, 20],[15, 88, 1, 5, 7],[1, 10, 15, 5, 20]));
OUTPUT
[5, 10, 15, 20]
How can I access all sub-arrays of a main array inside this function?

You need to pass array with the bracket [].
function intersection(arrays) {
return arrays;
}
console.log(intersection([[5, 10, 15, 20],[15, 88, 1, 5, 7],[1, 10, 15, 5, 20]]));

You can use rest parameters with Array#flat.
function intersection(...arrays) {
return arrays.flat();
}
console.log(intersection([5, 10, 15, 20],[15, 88, 1, 5, 7],[1, 10, 15, 5, 20]));

While I suggest one of the other answers, if you don't want to change the function signature, nor the calling code, you can use Array.from(arguments) in the code to get all of the arguments passed.
function intersection(arrays) {
return Array.from(arguments);
}
console.log(intersection([5, 10, 15, 20],[15, 88, 1, 5, 7],[1, 10, 15, 5, 20]));

You're trying to pass multiple arguments to the function. But the function accepts a single argument. That means that the only the first array in the function will be passed.
With the rest operator you can turn a single parameter into an array of parameters with an indefinite length. This way you can add arrays like the way you're doing.
function intersection(...arrays) {
return arrays;
}
const result = intersection(
[5, 10, 15, 20],
[15, 88, 1, 5, 7],
[1, 10, 15, 5, 20]
);
console.log(result);
Or pass the arrays as a multidimensional array.
function intersection(arrays) {
return arrays;
}
const result = intersection(
[
[5, 10, 15, 20],
[15, 88, 1, 5, 7],
[1, 10, 15, 5, 20]
]
);
console.log(result);

Related

Why filter function is returning array even when it does not pass the condition

I'm struggling to understand why this code is not working. its working with a small dataset though.
const gl = wList.GreateLeague
const data = JSON.parse(fs.readFileSync("./raw/data.json"))
(function () {
let glFinds = data.pokemons.filter((poke) => gl.hasOwnProperty(poke.pokemon_id))
let ax = glFinds.filter((p) => {
return gl[p.pokemon_id].stats.filter((ammo) => {
return (
p.attack === ammo[0] && p.defence === ammo[1] && p.stamina === ammo[2]
);
});
});
console.log(glFinds)
console.log(ax)
})();
my output for both console is same, therefore filter on ax is not working.
ax is working fine if I include testData.json which is a smaller then data.json.
edit: stats look like this. here [0],[1],[2] are the attack defense and stamina
"stats": [
[1, 15, 15, 20.5, 1494],
[0, 13, 11, 20.5, 1494],
[2, 14, 15, 20.5, 1494],
[0, 10, 15, 20.5, 1494],
[1, 12, 11, 20.5, 1494],
[0, 15, 15, 20.5, 1494],
[3, 13, 15, 20.5, 1494],
[0, 14, 10, 20.5, 1494],
[2, 15, 14, 20.5, 1494],
[0, 11, 13, 20.5, 1494]
],
my data files have array of objects like this:
{
"pokemon_id": 155,
"attack": 2,
"defence": 5,
"stamina": 0,
"move1": 209,
"move2": 125,
"level": 4,
}
I am trying to filter any object that matches (0,1,2 index) of stats array, object's attack, defense and stamina must match any of the stats array first 3 element.
The function inside filter must return true or false, depending on whether the current item shall be included in the output array or not. But your function returns gl[p.pokemon_id].stats.filter(...), which is an array, as Andreas already pointed out.
When trying to evaluate an array as true or false, it counts as true (even if it is empty), therefore your whole filter procedure effectively does nothing.
if ([]) console.log(true);
// true

How to make nested array into a single array in javascript

I got stuck in some problem and unable to get any idea of how to resolve it.As I have a nested
array given below:
arr = [1,2,3,4,5,6,[7,8,9,[10,[21,22,[24,25,26],23],11],12,13],14,15,16,[17,18],19,20]
On using flat() method below method:
arr.flat()
it gives below output:
[1,2,3,4,5,6,7,8,9,[ 10, [ 21, 22, [Array], 23 ],11 ],12,13,14,15,16,17,18,19,20]
How can I convert this nested array into a single array.Someone let me know.
You can use the flat method here.
arr.flat(Infinity);
const arr = [
1,
2,
3,
4,
5,
6, [7, 8, 9, [10, [21, 22, [24, 25, 26], 23], 11], 12, 13],
14,
15,
16, [17, 18],
19,
20,
];
const result = arr.flat(Infinity);
console.log(result);

Mine loop doesn't work in Javascript. I have to traverse loop to array and multiply by 2

hey guys i have problem with my exercise:
"using a while loop, traverse the array and multiply each price by 2" i have this to now
var prices = [10, 15, 25, 8, 4, 55, 99, 11, 15, 25, 5, 4, 65, 5, 10, 15, 7, 8, 4, 9, 100];
while (prices < 201) {
console.log('This item costs', prices);
prices * 2
}
I don't know where the error is?
Right now you are just doing for one element. In array you have to access element with their index number. The logic for doing is that you have to loop till the end of the array and for each element have to multiply the price by 2.
I have attached the code below with proper logic.
var prices = [10, 15, 25, 8, 4, 55, 99, 11, 15, 25, 5, 4, 65, 5, 10, 15, 7, 8, 4, 9, 100];
var i=0;
//loop to double price of each element in the array
while (i < prices.length){
prices[i]=prices[i]*2;
i++;
}
console.log(prices);

How to remove last element from every row of a matrix in javascript

I am trying to remove the last element from every row of a matrix in javascript. I am trying to use the "map" function but I am not successful.
Here is my code:
var matrixWithExtraInfo = [
[1, 2, 3, 4, "dog"],
[5, 6, 7, 8, "dog"],
[9, 10, 11, 12, "dog"],
[13, 14, 15, 16, "dog"],
[17, 18, 19, 20, "dog"]
];
var conciseMatrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]
]
var conciseMatrix = matrixWithExtraInfo.map(function(index) {
console.log(index)
matrixWithExtraInfo[index].pop();
return matrixWithExtraInfo[index];
});
console.log(matrixWithExtraInfo);
I get
TypeError: Cannot read property 'pop' of undefined
The first argument to .map is the item you're iterating over, not the index.
Since each item here is an array, you can either .pop the array (which will mutate the existing array), or .slice the array (which will not mutate the existing array).
var matrixWithExtraInfo = [
[1,2,3,4,"dog"],
[5,6,7,8,"dog"],
[9,10,11,12,"dog"],
[13,14,15,16,"dog"],
[17,18,19,20,"dog"]
];
var conciseMatrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16],
[17,18,19,20]
]
var conciseMatrix = matrixWithExtraInfo.map((arr) => {
arr.pop();
return arr;
});
console.log(matrixWithExtraInfo);
console.log(conciseMatrix);
(the above is weird - the structure you need is already in matrixWithExtraInfo, making another variable to hold it is confusing, but this is the closest to your original code)
var matrixWithExtraInfo = [
[1,2,3,4,"dog"],
[5,6,7,8,"dog"],
[9,10,11,12,"dog"],
[13,14,15,16,"dog"],
[17,18,19,20,"dog"]
];
var conciseMatrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16],
[17,18,19,20]
]
var conciseMatrix = matrixWithExtraInfo.map(arr => arr.slice(0, -1));
console.log(matrixWithExtraInfo);
console.log(conciseMatrix);
If you want to mutate the existing array, an easy way is to use Array.forEach() to apply Array.pop() to each element:
var matrixWithExtraInfo = [
[1, 2, 3, 4, "dog"],
[5, 6, 7, 8, "dog"],
[9, 10, 11, 12, "dog"],
[13, 14, 15, 16, "dog"],
[17, 18, 19, 20, "dog"]
];
matrixWithExtraInfo.forEach(arr => arr.pop());
console.log(matrixWithExtraInfo);
Otherwise, just use Array.slice():
var matrixWithExtraInfo = [
[1, 2, 3, 4, "dog"],
[5, 6, 7, 8, "dog"],
[9, 10, 11, 12, "dog"],
[13, 14, 15, 16, "dog"],
[17, 18, 19, 20, "dog"]
];
var conciseMatrix = matrixWithExtraInfo.map(arr => arr.slice(0, -1));
console.log(conciseMatrix);
Map will return the element so by iterating all rows we just pop the element which will remove the last element from the each row. As we need a new matrix in different array we should use slice(not pop/splice) which will return the elements in new array and not modifying original matrix.
var matrixWithExtraInfo = [
[1,2,3,4,"dog"],
[5,6,7,8,"dog"],
[9,10,11,12,"dog"],
[13,14,15,16,"dog"],
[17,18,19,20,"dog"]
];
var conciseMatrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16],
[17,18,19,20]
]
var conciseMatrix = matrixWithExtraInfo.map(row => row.slice(0, row.length-1));
console.log(matrixWithExtraInfo);
console.log(conciseMatrix);
Just adding _, in map function in your code should fix. function(_,index)
var matrixWithExtraInfo = [
[1, 2, 3, 4, "dog"],
[5, 6, 7, 8, "dog"],
[9, 10, 11, 12, "dog"],
[13, 14, 15, 16, "dog"],
[17, 18, 19, 20, "dog"]
];
var conciseMatrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]
]
var conciseMatrix = matrixWithExtraInfo.map(function(_,index) {
console.log(index)
matrixWithExtraInfo[index].pop();
return matrixWithExtraInfo[index];
});
console.log(matrixWithExtraInfo);

jquery multidimensional array shuffle random

I want to minimize my code from:
myArrayA = [1, 2, 3, 4, 5];
fisherYates(myArrayA);
myArrayB = [6, 7, 8, 9, 10];
fisherYates(myArrayB);
myArrayC = [11, 12, 13, 14, 15];
fisherYates(myArrayC);
myArrayD = [16, 17, 18, 19, 20];
fisherYates(myArrayD);
myArrayE = [21, 22, 23, 24, 25];
fisherYates(myArrayE);
To:
var multArr = [[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]];
fisherYates(multArr);
The output I want is like this:
[4,2,3,5,1],[7,10,6,9,8],[11,15,12,14,13],[18,17,16,20,19],[22,21,25,23,24]
I tried this code:
http://jsfiddle.net/arrow/yFn8U/
function fisherYates(myArray) {
var i = myArray.length, j, tempi, tempj;
if (i === 0) return false;
while (--i) {
j = Math.floor(Math.random() * (i + 1));
tempi = myArray[i];
tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}
var multArr = [[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]];
fisherYates(multArr);
But my code only randomizes the order of the chunks not the values in each chunk.
The output I want is like this:
[4,2,3,5,1],[7,10,6,9,8],[11,15,12,14,13],[18,17,16,20,19],[22,21,25,23,24]
I want each chunk inside the array to be in the same order but each chunk must be randomized.
Is there a way to do this with jQuery?
I also wonder how to get values from the shuffled/randomized array?
At the moment I get the values like this:
myArrayA[i]
myArrayB[i]
myArrayC[i]
myArrayD[i]
myArrayE[i]
I would guess I will get them with something like:
multArr [[0][i]];
multArr [[1][i]];
multArr [[2][i]];
multArr [[3][i]];
multArr [[4][i]];
Finally I wonder if minimizing the code will give better performance?
If you simply want to run an operation over all the elements in an array, then you should use map or forEach. I'm sure jquery provides shims for these methods in older browsers. So if we assume you're using your original fisherYates function unaltered, we might have something like this:
var multArr = [[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]];
multArr.forEach(fisherYates);
On accessing the elements, you're almost right, but you have one set too many of brackets :
multArr[1]; // == [6, 7, 8, 9, 10]
multArr[1][3]; // == 9
I wouldn't speculate about the performance, if you're really worried you should put together a jsperf test case.
All you need is jQuery's .each() method, like so:
$.each(multArr, function(i) { fisherYates(this) });
See console on this working example
Fiddle Code
function fisherYates(myArray) {
var i = myArray.length, j, tempi, tempj;
if (i === 0) return false;
while (--i) {
j = Math.floor(Math.random() * (i + 1));
tempi = myArray[i];
tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}
$(function() {
$("button").on("click", function(e) {
multArr = [[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]];
$.each(multArr, function(i) { fisherYates(this) });
console.log(multArr)
})
})
Check out my code here. Basically just looped over the elements of the multidimensional array and run the fisherYates on them like so:
function fisherYates(myArray) {
for(var i = 0; i< myArray.length; i++) {
k = myArray[i].length;
while(k--){
j = Math.floor(Math.random() * (myArray.length - 1));
tempk = myArray[i][k];
tempj = myArray[i][j];
myArray[i][k] = tempj;
myArray[i][j] = tempk;
}
}
}
Now if you wanted to do this for an n-dimensional array you're going to have to do it recursively, which would be fun, but I think that is more than you were asking for. If not I can update it later.

Categories