1

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?

7
  • If you want to continue the same code only then you have to put 1 more loop to process multi array. Commented Nov 5, 2019 at 7:09
  • please give me an example of the source code :) Commented Nov 5, 2019 at 7:14
  • what is 'src1' ? Commented Nov 5, 2019 at 7:15
  • @AnkitPandey I think it's supposed to be the fruits array. Commented Nov 5, 2019 at 7:18
  • oh sorry, it should be: alert (searchStringInArray (fruits, 'Grape')) Commented Nov 5, 2019 at 7:19

4 Answers 4

1

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'));

Sign up to request clarification or add additional context in comments.

2 Comments

Really, this isn't the best way to perform the operation; you should be using something like filter or find. If you're going to do it manually, then try a binary approach to the test, so you're not looking at every single element.
@Paul, Thanks for the comment. But I am looking at all elements till not found.
1

You can use Array.prototype.find() combined with Array.prototype.includes():

const fruits = [
  ["001", "Apple", "For John"],
  ["002", "Banana", "For Stuart"],
  ["003", "Grape", "For Collins"],
  ["004", "Orange", "For Ben"]
]

const searchStringInArray = (src1, searched) => src1.find(arr => arr.includes(searched))
  
console.log(searchStringInArray(fruits, 'Grape'))

Comments

0
for(f of fruits){data=f.filter(s => s==="Apple").length > 0;
console.log(data); if(data){console.log("found");filteredD.push(f);}}

Comments

0

It is possible to flat your multidimensional array and then find desired item:

const fruits= [
    ["001","Apple","For John"]
    ,[
        ["002","Banana","For Stuart"],
        ["003","Grape","For Collins"],
        ["004","Orange","For Ben"]
    ]
];

const flatArray = (arr) => {
    return arr.reduce(function (flat, toFlatten) {
      return flat.concat(Array.isArray(toFlatten) ? flatArray(toFlatten) : toFlatten);
    }, []);
  }

const flattened = flatArray(fruits);
const fruitToFind = 'Grape';
const result = flattened.find(f=> f === fruitToFind);
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.