please help me, I have a function to search for one-dimensional arrays, here are the source codes and their variables:
var fruits= ["Apple","Banana","Grape","Orange"];
function searchStringInArray (src1,fruits)
{
var res=""
for (var j=0; j<src1.length; j++) {
if (src1[j].match (searched))
{
res=res+src1[j]+","
}
}
return res;
}
alert(searchStringInArray (src1,'Grape'));
but I want to modify the function to search for multidimensional arrays, with the following variables:
var fruits= [
["001","Apple","For John"],[["002","Banana","For Stuart"],["003","Grape","For Collins"],["004","Orange","For Ben"]
];
Approximately how the function is suitable for the variable?
A more convenient way to use this with the prototype.
const fruits = [
["001", "Apple", "For John"],
["002", "Banana", "For Stuart"],
["003", "Grape", "For Collins"],
["004", "Orange", "For Ben"]
]
Array.prototype.searchIn2DArray = function(fruitsName){
let searched = false;
for(let j=0; j<this.length; j++){
if(this[j].indexOf(fruitsName) > -1){
return this[j];
}
}
return searched;
}
console.log(fruits.searchIn2DArray('Orange'));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments