I have a 2d array like this:
var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]];
Each index stores an inner array containing the coordinates of some
Here is a solution done using prototype so the usage resembles that of indexOf but for 2d arrays. Use in the same way: arr.indexOf2d([2,3]);
var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]];
Array.prototype.indexOf2d = function(item) {
// arrCoords is an array with previous coordinates converted to strings in format "x|y"
arrCoords = JSON.stringify(this.map(function(a){return a[0] + "|" + a[1]}));
// now use indexOf to find item converted to a string in format "x|y"
return arrCoords.indexOf(item[0] + "|" + item[1]) !== -1;
}
arr.indexOf2d([2,3]); // true
arr.indexOf2d([1,1]); // true
arr.indexOf2d([6,1]); // false
Here's what I implemented
getIndexOfArray(array: any[], findArray: any[]): number{
let index = -1;
array.some((item, i)=>{
if(JSON.stringify(item) === JSON.stringify(findArray)) {
index = i;
return true;
}
});
return index;
}
here array
is the array in which we need the index and findArray
is the array whose index will be returned.
Note: This function will return only the first occurrence of findArray
array insight array
array.
Very simple without indexOf...
var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]];
const isDup = (x,y) => {
arr.find(it => JSON.stringify(it) == JSON.stringify([x,y])) == undefined ? arr.push([x,y]) : null
}
console.log(isDup(2,3)) /* Does not add */
console.log(isDup(1,2)) /*Does add*/
console.log(arr) /*Confirmation*/
you can use this method,
function isArrayItemExists(array , item) {
for ( var i = 0; i < array.length; i++ ) {
if(JSON.stringify(array[i]) == JSON.stringify(item)){
return true;
}
}
return false;
}
A previous answer said:
You cannot use indexOf to do complicated arrays (unless you serialize it making everything each coordinate into strings)...
Here's how you'd do just that. If you have an extremely large data set, I'd recommend against this technique, as it relies on a duplicate of your 2D array. But for reasonable sets, it's simple.
Use a consistent method for flattening array elements, such as:
// Flatten array into a string, separating elements with a "unique" separator.
function stringle( arr ) {
return arr.join(' |-| ');
}
This is overkill for your example, where sub-arrays contain integers, but it accounts for more complex data types. (If we used a comma, the default, it would be indiscernible from a string element that contained a comma, for example.)
Then the target array can be flattened into an array of strings:
// Transmogrify arr into a String[], usable with indexOf()
var arrSearch = arr.map(function(row) { return stringle(row); });
Then you can use Array.indexOf()
(or other array methods) to check for the presence or location of matches.
if (arrSearch.indexOf( stringle(newArray) ) === -1) ...
This snippet contains a demo of this, with multiple data types.
// Example starting array
var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]];
// Flatten array into a string, separating elements with a "unique" separator.
function stringle( arr ) {
return arr.join(' |-| ');
}
snippet.log("arr: "+JSON.stringify(arr));
// Transmogrify arr into a String[], usable with indexOf()
var arrSearch = arr.map(function(row) { return stringle(row); });
snippet.log("arrSearch: "+JSON.stringify(arrSearch));
var tests = [[0, 9],[1, 2],["pig","cow"],[0,9,"unicorn"],["pig","cow"]];
for (var test in tests) {
var str = stringle(tests[test]);
if (arrSearch.indexOf(str) === -1) {
arr.push(tests[test]);
arrSearch.push(str);
snippet.log("Added "+JSON.stringify(tests[test]));
}
else {
snippet.log("Already had "+JSON.stringify(tests[test]));
}
}
snippet.log("Result: "+JSON.stringify(arr));
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Working js fiddle
for(var k = 0; k < arr.length; k++){
if(arr[k][0] == x && arr[k][1] == y){
found = true;
}
}
Much more of a hacky way than a simple index of, but it works