Can't remove element using splice - javascript

I have an array with length 9. I want to remove elem from array.
arr = [
[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
[2, 0],
[2, 1],
[2, 2]
]
let elem = [2, 0];
let index = arr.indexOf(elem)
if (index !== -1) {
arr.splice(arr.indexOf(elem), 1)
}
console.log(arr)
Why my splice does not work?

It is not possible to use Array#indexOf with another object reference than the same object reference. This means, if you have another array to check against, you need to iterate it element by element. For this, you could use Array#findIndex and iterate all elements with Array#every.
For splicing, you need the found index, and not to find another index, because you have it already.
var arr = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]],
elem = [2, 0],
index = arr.findIndex(a => a.every((v, i) => elem[i] === v));
console.log(index);
if (index !== -1) {
arr.splice(index, 1);
}
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can use .filter() instead: (more about .filter() here)
arr = [
[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
[2, 0],
[2, 1],
[2, 2]
]
let elem = [2, 0];
const newArray = arr.filter(item => !(item[0] === elem[0] && item[1] === elem[1]));
console.log(newArray)
.as-console-wrapper { max-height: 100% !important; top: 0; }
This simply checks if both parts of the array match, then reverses the result, so only those that DON'T match are returned
Not that it makes much difference nowadays but .filter() is also faster - Here's a test I ran on JSPerf

You can use the filter function
arr = [
[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
[2, 0],
[2, 1],
[2, 2]
]
let m = [];
let elem = [2, 0];
let __j = elem.toString();
let k = arr.filter(function(item) {
let __i = item.toString();
return __i !== __j
})
console.log(k)

You can use JSON object to remove quickly a element:
arr = [
[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
[2, 0],
[2, 1],
[2, 2]
]
let elem = [2, 0];
var arrjson = arr.map(JSON.stringify);
var elemjson = [elem].map(JSON.stringify);
//console.log(arrjson); //console.log(elemjson);
var index = arrjson.indexOf(elemjson[0]);
arrjson.splice(index,1);
//console.log(arrjson);
var arrresult = arrjson.map(JSON.parse);
console.log(arrresult);

Related

To get a truthy condition(array) from a nested array, for now the function returns boolean value true

The checkWin function returns true when an array which are index numbers have a speicified symbol on another Array's indexes. How to retrieve the "cond"(array) which results in true from the nested winConditions array ?
Symbols get populated using the click event listener.
The expected result should be, if .some(cond) becomes true then return that cond for eg. if symbol "X" is present on [0, 1, 2] then return this array
let testArray = Array(9).fill("")
const winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
let xValue = "X";
let oValue = "O";
function checkWin(value, array) {
return winConditions.some((cond) =>
cond.every((index) => array[index] == value));
}
console.log(checkWin(xValue, testArray));
console.log(checkWin(oValue, testArray));
You can use .find() instead of .some(), this will return the first array that the .every() callback returns true for. You can use this array to determine if there was a win or not for a particular player:
const testArray = Array(9).fill("");
testArray[3] = testArray[4] = testArray[5] = "O"; // test winning position
const winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
const xValue = "X";
const oValue = "O";
function getWinPos(value, array) {
return winConditions.find((cond) =>
cond.every((index) => array[index] == value)
);
}
const xWinPos = getWinPos(xValue, testArray);
const oWinPos = getWinPos(oValue, testArray);
if(xWinPos) { // found a winning position row/col/diag for "X"
console.log(xWinPos);
} else if(oWinPos) { // found a winning row/col/diag for "O"
console.log(oWinPos);
}

How can I turn coordinates to two dimensional array in javascript

in my project I have to turn a bunch of coordinates to some meaningful two-dimensional array but I really don't know how to do it. Can somebody help?
To explain what I exactly want, let me give an example:
Let's suppose that I have these 2 arrays(the reason that I started from one is because 0 and the last element of my rows are borders):
[[1, 1], [1, 2], [1, 4], [1, 5], [1, 8], [1, 9], [1, 10],[2, 1], [2, 2], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 10]]
Let the value inside these coordinates be like [row,col]. And let's say I wan't to match them to generate some sort of two-dimensional array and each of the elements should contain the value '#'. However, for example;
[1, 2], [1, 4]
[2, 2], [2, 4]
If there's a coordinate missing between two of these elements, they should be separated, meaning that there should be two different two-dimensional arrays, being split from that coordinate. In this case, the result should be;
// First two-dimensional array
const firstArray = [
['#','#'],
['#','#']
]
const secondArray = [
['#','#','','','#','#','#'],
['#','#','#','#','#','','#'],
]
In the second array, there are some '' values, but that is because the there are some coordinates missing(for [1, 5] and [1, 8], [1,6] and [1,7] are missing). So that should be considered too.
If you didn't understand please comment under the question me so that I can explain it to you.
How can I come up with the functionality that I'm looking for?
You can accomplish both steps with a single Array#reduce() call by using the coordinates themselves to place each [row, col] in its relevant place in the matrix.
Here using an OR short circuit for assigning new sub-arrays, with a commented out replacement using the logical nullish assignment operator (??=), and the comma operator for shorthand return in the arrow function.
const coords = [[1, 1], [1, 2], [1, 4], [1, 5], [1, 8], [1, 9], [1, 10], [2, 1], [2, 2], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 10]];
const matrix = coords.reduce((acc, [row, col]) => (
// using OR short circuit for compatibility
(acc[row - 1] || (acc[row - 1] = []))[col - 1] = [row, col], acc
// using logical nullish assignment operator (??=)
//(acc[row - 1] ??= [])[col - 1] = [row, col], _matrix
), [])
// logging
for (const row of matrix) {
console.log(`[[${row.join('], [')}]]`)
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
const input = [[1, 1], [1, 2], [1, 4], [1, 5], [1, 8], [1, 9], [1, 10],[2, 1], [2, 2], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 10]]
const result = input.reduce((acc, [x, y]) => {
acc[x - 1] ??= []
const previousY = acc[x - 1][acc[x-1].length - 1];
if (previousY) {
const delta = y - previousY;
if (delta > 1) acc[x-1].push(...Array.from({length: delta - 1}));
}
acc[x-1].push(y);
return acc
}, [])
console.log('1234567890')
console.log(
result.map(row =>
row.map(coor => coor ? '#' : ' ').join('')
).join('\n'))

Finding closest combination to current ongoing one

I'm currently building a tic tac toe in vanilla javascript. However the game is 'sort of' done but I'm trying to add levels of difficulty. So basically the thing I want to do is , on every player move , to get the the closest possible winning combination based on his moves and place computer's mark into the missing winning's combinations place.
Let's say I have multidimensional array with the winning combinations
winningCombinations: [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[0, 3, 6],
[1, 4, 7],
[2, 4, 6],
[2, 5, 8]
]
And the player X starts his moves. So his first move is 0, so saving player's current combination in array. So in first move the current comb is
currentPlayerCombintation: [0]
so I want to return [0,1,2], [0,4,8] and [0,3,6] from the winning combination's array.
However the player makes his second move , so he target's 4 so
currentPlayerCombination: [0,4]
and now I want to return the closest possible winning combination which is [0,4,8].
I've tried a lot of things including every() , some() , filter() but could not achieve the thing I want.
I've tried sort of
for(let i = 0; i < this.currentPlayerCombination.length ; i++) {
this.winningCombinations.some((arr) => {
if(arr.includes(this.currentPlayerCombination[i])) {
console.log(arr);
}
});
}
But this didnt work as expected :(
You could take a Set and map the count of the matching items, get the max count and filter the array.
function getWinningPositions(pos) {
var posS = new Set(pos),
temp = winningCombinations.map(a => [a, a.reduce((c, v) => c + posS.has(v), 0)]),
max = Math.max(...temp.map(({ 1: c }) => c))
return temp
.filter(({ 1: c }) => c === max)
.map(([a]) => a);
}
var winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 4, 8], [0, 3, 6], [1, 4, 7], [2, 4, 6], [2, 5, 8]];
console.log(getWinningPositions([0]).map(a => a.join(' ')));
console.log(getWinningPositions([0, 4]).map(a => a.join(' ')));
console.log(getWinningPositions([0, 4, 5]).map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
First map the winningCombinations to an array of arrays whose numbers are only the numbers that have not been picked yet. Then, find the lowest length of those arrays, and you can identify the original winningCombinations which are closest to the currentPlayerCombination:
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[0, 3, 6],
[1, 4, 7],
[2, 4, 6],
[2, 5, 8]
];
const currentPlayerCombination = [0, 4];
// eg: get [[1, 2], [3, 5,], [6, 7, 8], [8], ...]
const winningCombsWithoutCurrent = winningCombinations.map(arr => (
arr.filter(num => !currentPlayerCombination.includes(num))
));
// eg: here, lowestLength should be 1, because [8] has a length of 1
const lowestLength = winningCombsWithoutCurrent.reduce((a, { length }) => Math.min(a, length), 3);
const combosWithLowestLength = winningCombsWithoutCurrent
.reduce((a, { length }, i) => {
if (length === lowestLength) {
a.push(winningCombinations[i]);
}
return a;
}, []);
console.log(combosWithLowestLength);

Shift entire column in multidimensional array using only javascript or ES6

I have a multidimensional array like below and I want to shift column positions using javascript or ES6 with no jquery or any other plugins.
Eg: Initial array will look like this.
1|2|3|4
2|2|6|4
4|2|3|4
9|2|7|4
How can I shift the 4th column to 1st position so that it will look like this?
4|1|2|3
4|2|2|6
4|4|2|3
4|9|2|7
Could someone can help with logic to shift any columns like this?
You could assing a mapped outer array with new items by slicing the inner arrays with a given index.
For getting the original sort, you could shiftby the delta of length and index.
const shift = (array, index) => array.map(a => [...a.slice(index), ...a.slice(0, index)]);
var array = [[1, 2, 3, 4], [2, 2, 6, 4], [4, 2, 3, 4], [9, 2, 7, 4]],
index = 3;
array = shift(array, index);
console.log(array.map(a => a.join(' ')));
array = shift(array, array[0].length - index);
console.log(array.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use array.map to re-arrange the values:
function rearrange(rows, pos) {
return rows.map(function(cols) {
return pos.map(function(i) {
return cols[i];
});
});
}
var old_arr;
var new_arr;
old_arr = [
[1, 2, 3, 4],
[2, 2, 6, 4],
[4, 2, 3, 4],
[9, 2, 7, 4]
];
new_arr = rearrange(old_arr, [3, 0, 1, 2]);
console.log(new_arr);
old_arr = [
[1, 2, 3, 4],
[2, 2, 6, 4],
[4, 2, 3, 4],
[9, 2, 7, 4]
];
new_arr = rearrange(old_arr, [3, 2, 1, 0]);
console.log(new_arr);

Find if two arrays are repeated in array and then select them

I have multiple arrays in a main/parent array like this:
var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
here are the array's for simpler reading:
[1, 17]
[1, 17]
[1, 17]
[2, 12]
[5, 9]
[2, 12]
[6, 2]
[2, 12]
[2, 12]
I want to select the arrays that are repeated 3 or more times (> 3) and assign it to a variable. So in this example, var repeatedArrays would be [1, 17] and [2, 12].
So this should be the final result:
console.log(repeatedArrays);
>>> [[1, 17], [2, 12]]
I found something similar here but it uses underscore.js and lodash.
How could I it with javascript or even jquery (if need be)?
Try this
array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))
var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))
console.log(JSON.stringify(r));
Time complexity O(n) (one array pass by filter function). Inspired by Nitish answer.
Explanation
The (r={}, a=>...) will return last expression after comma (which is a=>...) (e.g. (5,6)==6). In r={} we set once temporary object where we will store unique keys. In filter function a=>... in a we have current array element . In r[a] JS implicity cast a to string (e.g 1,17). Then in !(2-(r[a]=++r[a]|0)) we increase counter of occurrence element a and return true (as filter function value) if element a occurred 3 times. If r[a] is undefined the ++r[a] returns NaN, and further NaN|0=0 (also number|0=number). The r[a]= initialise first counter value, if we omit it the ++ will only set NaN to r[a] which is non-incrementable (so we need to put zero at init). If we remove 2- as result we get input array without duplicates - or alternatively we can also get this by a=>!(r[a]=a in r). If we change 2- to 1- we get array with duplicates only.
UPDATE
Even shorter version based on #ken comment can be written (it should always work with arrays of numbers). The original longer version of #ken code is in snippet and shows how #ken uses in clever way second argument of .filter to avoid usage global variable r.
array.filter(a=>!(2-(this[a]=++this[a]|0)))
var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
var r= array.filter(a=>!(2-(this[a]=++this[a]|0)), {})
console.log(JSON.stringify(r));
You could take a Map with stringified arrays and count, then filter by count and restore the arrays.
var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
result = Array
.from(array.reduce(
(map, array) =>
(json => map.set(json, (map.get(json) || 0) + 1))
(JSON.stringify(array)),
new Map
))
.filter(([, count]) => count > 2)
.map(([json]) => JSON.parse(json));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Filter with a map at wanted count.
var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
result = array.filter(
(map => a =>
(json =>
(count => map.set(json, count) && !(2 - count))
(1 + map.get(json) || 1)
)
(JSON.stringify(a))
)
(new Map)
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Unique!
var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
result = array.filter(
(s => a => (j => !s.has(j) && s.add(j))(JSON.stringify(a)))
(new Set)
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use Object.reduce, Object.entries for this like below
var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
let res = Object.entries(
array.reduce((o, d) => {
let key = d.join('-')
o[key] = (o[key] || 0) + 1
return o
}, {}))
.flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : [])
console.log(res)
OR may be just with Array.filters
var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
let temp = {}
let res = array.filter(d => {
let key = d.join('-')
temp[key] = (temp[key] || 0) + 1
return temp[key] == 3
})
console.log(res)
For a different take, you can first sort your list, then loop through once and pull out the elements that meet your requirement. This will probably be faster than stringifying keys from the array even with the sort:
var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])
// define equal for array
const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])
let GROUP_SIZE = 3
first = 0, last = 1, res = []
while(last < arr.length){
if (equal(arr[first], arr[last])) last++
else {
if (last - first >= GROUP_SIZE) res.push(arr[first])
first = last
}
}
if (last - first >= GROUP_SIZE) res.push(arr[first])
console.log(res)
You could also do this with a single Array.reduce where you would only push to a result property if the length is equal to 3:
var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
console.log(array.reduce((r,c) => {
let key = c.join('-')
r[key] = (r[key] || 0) + 1
r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
return r
}, { result: []}).result) // print the result property
ES6:
const repeatMap = {}
array.forEach(arr => {
const key = JSON.stringify(arr)
if (repeatMap[key]) {
repeatMap[key]++
} else {
repeatMap[key] = 1
}
})
const repeatedArrays = Object.keys(repeatMap)
.filter(key => repeatMap[key] >= 3)
.map(key => JSON.parse(key))

Categories

Resources