How do I create a function for two-dimensional array search?

Benz Iskan

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?

Ankit Pandey

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use a for loop to create a two dimensional array and assign linear values with an increment of 10?

How can I create a two dimensional array in JavaScript?

How do I create a primitive two-dimensional (2d) array of doubles in Clojure?

how to create dynamic two dimensional array in java?

How do you rotate a two dimensional array?

How do I call a Clojure function that takes a two-dimensional array of Strings from Java?

Two dimensional array as a function

How to search within a two-dimensional array

How can I create a simple 4x3 two dimensional array in Java?

How to create two dimensional array in Promela?

How do I print the number of a row in a two dimensional array?

How do I match numbers and create 2-dimensional array?

How do I initialize a two-dimensional array in C

How to create two dimensional array with 2 arrays

How to create html from a two dimensional array?

Error in referencing two dimensional array in function search for pattern

How to create a two dimensional array and add values to it?

In java how do I put two one-dimensional arrays into one two-dimensional array?

How to create a two dimensional array in python

How do I handle a 2-dimensional array search when the key will have multiple entries?

How do I apply array methods to individual array that compose a two dimensional array in javascript?

How do I post a two-dimensional array to an API

How do I convert an array object into a two-dimensional array

How to create two dimensional array when I have an object in javascript?

Search in a two-dimensional array

How do I copy a two dimensional array with null at the end?

How can I print a two-dimensional array of characters in Java, to create a 20x20 grid?

How can I use numpy to create a function that returns two-dimensional values from arrays of data?

How can I create a two dimensional array with each element being a list in Java?