Related
I have a 2d array in the following format:
export const dataBubble = [
[0, 0, 0],
[0, 1, 0],
[0, 2, 0],
[0, 3, 0],
[0, 4, 0],
[0, 5, 0],
[0, 6, 0],
[0, 7, 0],
[0, 8, 0],
[0, 9, 0],
[0, 10, 0],
[1, 0, 6],
[1, 1, 8],
[1, 2, 5],
[1, 3, 6],
[1, 4, 1],
[1, 5, 4],
[1, 6, 5],
[1, 7, 5],
[1, 8, 4],
[1, 9, 3],
[1, 10, 9],
[2, 0, 5],
[2, 1, 5],
[2, 2, 5],
[2, 3, 6],
[2, 4, 8],
[2, 5, 7],
[2, 6, 8],
[2, 7, 5],
[2, 8, 4],
[2, 9, 2],
[2, 10, 8],
[3, 0, 9],
[3, 1, 5],
[3, 2, 9],
[3, 3, 8],
[3, 4, 5],
[3, 5, 4],
[3, 6, 2],
[3, 7, 5],
[3, 8, 7],
[3, 9, 6],
[3, 10, 3],
[4, 0, 7],
[4, 1, 3],
[4, 2, 9],
[4, 3, 5],
[4, 4, 11],
[4, 5, 6],
[4, 6, 7],
[4, 7, 6],
[4, 8, 4],
[4, 9, 4],
[4, 10, 5],
[5, 0, 1],
[5, 1, 3],
[5, 2, 6],
[5, 3, 8],
[5, 4, 5],
[5, 5, 5],
[5, 6, 4],
[5, 7, 8],
[5, 8, 9],
[5, 9, 2],
[5, 10, 4],
[6, 0, 2],
[6, 1, 1],
[6, 2, 0],
[6, 3, 3],
[6, 4, 8],
[6, 5, 5],
[6, 6, 6],
[6, 7, 2],
[6, 8, 5],
[6, 9, 6],
[6, 10, 4],
[7, 0, 1],
[7, 1, 0],
[7, 2, 5],
[7, 3, 0],
[7, 4, 5],
[7, 5, 8],
[7, 6, 9],
[7, 7, 0],
[7, 8, 7],
[7, 9, 8]
];
We need to reverse the array elements based on the first two elements of the inner array, such that the resultant array becomes:
[
[0,10,0],
[0,9,0],
[0,8,0],
[0,7,0],
[0,6,0],
[0,5,0],
[0,4,0],
[0,3,0],
[0,2,0],
[0,1,0],
[1,10,9],
[1,9,3],
[1,8,4],
[1,7,5],
.
.
.
.
.
.
.
.
.
[7,0,1]
I use this in a react js project, so is there anyway we can use the JS map function of an array to do it? If not what will be the optimal solution?
If your second value is guaranteed to be less than an amount (for example I assumed 1000 in this case) then you can do this:
dataBubble.sort((x, y) => (x[0] - y[0])*1000 + y[1] - x[1]);
Basically we sum them up, but give the first element a higher coefficient, thus making it the primary sorter.
Otherwise, you need to involve an if check:
dataBubble.sort((x, y) => {
if(x[0] == y[0]) {
return y[1] - x[1];
} else {
return x[0] - y[0];
}
});
If you return a negative value from the sort function, JS will put x before y. Otherwise it will put x after y.
If x[0] and y[0] are equal, we need to sort depending on x[1] and y[1]. y[1] - x[1] will be negative if x[1] is larger, therefore if x[1] is larger x will come before y.
If x[0] and y[0] are different we need to sort based on them. x[0] - y[0] will be negative if x[0] is smaller, therefore if x[0] is smaller, x will come before y.
In ReactJS tutorial has a tutorial on building a tic tac toe game with React. There's this function that checks to see if a winning move is made.
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
it works but the for loop could be improved to use for x in lines like so
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let line in lines) {
const [a, b, c] = line;
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
but when I tried that it didn't find a winning so I resorted to the older code.
I'm not sure why though the second edit failed.
function calculateWinner(squares) {
const lines = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]];
for (let line in lines) {
console.log(line);
const [a, b, c] = line;
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
Run this code. Your line variable is index of array, not inner array (e.g. [0, 1, 2]).
EDIT:
As kristaps mentioned in comment proper solution is let line of lines (of instead of in).
here in your first code you are using for loop which can excess 2d array in proper way
example below program will produce out put like
[
0,
1,
2
]
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
console.log(lines[i]);
}
and also for each loop is use for excess each element at a time for example below program will produce output like
1
2
3
as it
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let line in lines) {
const [a, b, c] = line;
console.log(line)
}
i think you can understand what happened here
For example let's say I have arrays
a = [1, 2, 3, 4, 5, 6] and b = a
And the output I expect would be:
1 + 1, 1 + 2, 1 + 3 ... 3 + 1, 3 + 2, 3 + 4 ... 6 + 3, 6 + 4, 6 + 5, 6 + 6
I would prefer to make this simple calculation in JS or Ruby, but I don't mind answer in any other language. Can anyone provide me any direction?
In Ruby:
a.product(b).map {|p| p.reduce(:+) }.uniq
a = [1, 2, 3, 4, 5, 6]
b = [1, 4, -1, 7, 9]
a.product(b).map { |a,b| a+b }.uniq
#=> [2, 5, 0, 8, 10, 3, 6, 1, 9, 11, 4, 7, 12, 13, 14, 15]
The steps:
c = a.product(b)
#=> [[1, 1], [1, 4], [1, -1], [1, 7], [1, 9],
# [2, 1], [2, 4], [2, -1], [2, 7], [2, 9],
# [3, 1], [3, 4], [3, -1], [3, 7], [3, 9],
# [4, 1], [4, 4], [4, -1], [4, 7], [4, 9],
# [5, 1], [5, 4], [5, -1], [5, 7], [5, 9],
# [6, 1], [6, 4], [6, -1], [6, 7], [6, 9]]
d = c.map { |a,b| a+b }
#=> [2, 5, 0, 8, 10,
# 3, 6, 1, 9, 11,
# 4, 7, 2, 10, 12,
# 5, 8, 3, 11, 13,
# 6, 9, 4, 12, 14,
# 7, 10, 5, 13, 15]
d.uniq
#=> [2, 5, 0, 8, 10, 3, 6, 1, 9, 11, 4, 7, 12, 13, 14, 15]
In javascript, get unique sums using Set
var a = [1, 2, 3, 4, 5, 6];
var r = new Set();
a.forEach(x => a.forEach(y => r.add(x + y)))
document.write('<pre>' + Array.from(r) + '</pre>');
You can use Array.prototype.forEach() for the iteration over the arrays.
The forEach() method executes a provided function once per array element.
The result is without repeat.
function xSums(array) {
var r = [],
o = {};
array.forEach(function (a) {
array.forEach(function (b) {
if (!o[a + b]) {
r.push(b + a);
o[a + b] = true;
}
});
});
return r;
}
document.write('<pre>' + JSON.stringify(xSums([1, 2, 3, 4, 5]), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(xSums([3, 7, 42]), 0, 4) + '</pre>');
I would do something like this:
a = [1, 2, 3, 4, 5, 6]
a.permutation(2).map { |x, y| x + y }
Try looping through both arrays, adding the first to the second, and storing the results in a third, eg;
var one = [1, 2, 3];
var two = [4, 5, 6];
var three = [];
for(var x = 0; x < one.length; ×++){
for(var y = 0; y < two.length; y++){
three.push(one[x] + two[y]);
}
}
This would result in three[0] = 1 + 4, three[1] = 1+ 5, three[2] =1+ 6, three[3] = 2 + 4, three[4] = 2+ 5 etc...
I have an array that looks like:
var myArray = [12, 24, 36, 48, 60, 15, 30];
I need to build a new array of arrays from this to show the index number from the original array in the new array. The final result should look like the following:
var myNewArray = [
[1, 12],
[2, 24],
[3, 36],
[4, 48],
[5, 60],
[6, 15],
[7, 30]
];
You can use
Array.prototype.map(), and then generate new array based on value and index of that array.
Demo
var myArray = [12, 24, 36, 48, 60, 15, 30],
newArray = myArray.map(function (value, index) {
return [index + 1, value];
});
FYI: - JavaScript arrays are zero-indexed: the first element of an array is at index 0
var myArray= [12, 24, 36, 48, 60, 15, 30],
myArrayIndexed= myArray.map(function(itm, i){
return [i+1, itm];
});
myArrayIndexed.join(']\n[');
/* returned value: */ [
[1, 12],
[2, 24],
[3, 36],
[4, 48],
[5, 60],
[6, 15],
[7, 30]
]
It's as simple as that:
var myArray = [12, 24, 36, 48, 60, 15, 30];
var myNewArray = [];
for (var i = 0; i < myArray.length; i++) {
myNewArray.push([i+1,myArray[i]]);//or just i depending on the index you need
}
Even a faster way is to cache the length of the array:
for (var i = 0, var l = myArray.length; i < l; i++) {}
To my knowledge and research so far - Javascript's native for loop is quicker than array map for iterating through the array. Here is an interesting benchmark.
Hope this helps!
Simply put, is there a way to create a 2D javascript array using similar syntax to this?
var newArray = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
You can create any n-dimensional arrays using exactly the format you suggest as in the following sample:
<script>
var newArray = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
var newArray3d =
[[[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8]],
[[10, 11, 12],[13, 14, 15],[16, 17, 18]],
[[20, 21, 22],[23, 24, 25],[26, 27, 28]]]
alert(newArray[0]);
alert(newArray[0][2]);
alert(newArray3d[0]);
alert(newArray3d[1][0]);
alert(newArray3d[1][0][2]);
</script>
The alert boxes return, in sequence:
0,1,2
2
0,1,2,3,4,5,6,7,8
10,11,12
12
Yes. This works fine:
<script>
var newArray = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
alert(newArray[0][2]);
</script>
Tested and working in FF3, Opera 9, IE6, and Chrome.