Home > Software engineering >  loop through nested array and return item containing string from the first looped array
loop through nested array and return item containing string from the first looped array

Time:11-24

I have a nested array with data of file paths. What I was trying to do was get every item in the nested array and first check if the user inputted file name exists in the nested array and if it is found I wanted to log the whole path of that file name.

Currently, I have accomplished the first part but I can't figure out how to get the file path from the file name safely. Basically what I want is after the file name is verified by checking the nested array I want to log the file path which is the whole item in the nested array with that file name. How can I accomplish this?

One thing I have thought of is after verifying the file name run a loop of the nested array and compare every item by splitting it and checking if filename is found in that path. But is this method efficient for large arrays?

Plus note that the numbers in the path are dynamic so multiple items with the same file name but different identification numbers might occur.

c = [
  [
    ['dd\\32323232323:this1', 'dd\\43564564:this2'],
    ['dd\\5464656646:this3', 'dd\\43543453:this2']
  ]
]

var currentFileNames = []
var mainDataArrayFilenames = c.forEach((entrys) => {
  entrys.forEach((entry) => {
    entry.map(function(entry) {
      currentFileNames.push(entry.slice(entry.indexOf(':')   1))
    });
  })
})

function test() {
  const input = document.getElementById('input').value

  if (currentFileNames.includes(input) == true) {
    //expected full path of file name which is input
    console.log("path")
  } else {
    console.log("name not found")
  }
}
<input id="input">
<button onclick="test()">click</button>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Array.prototype.forEach won't return anything. You have to make use of Array.prototype.map if you want to store your result in a variable.

You can also flatten the Array using Array.prototype.flat.

c = [
  [
    ['dd\\32323232323:this1', 'dd\\43564564:this2'],
    ['dd\\5464656646:this3', 'dd\\43543453:this2']
  ]
]

var mainDataArray = c
  .flat(2) // flatten the array with depth of 3
  .map(itm => {
    try { // split each entry by :
      return itm.split(':');
    } catch(e) {
      console.error(e);
      return []
    }
  })
  .filter(arr => arr.length); // filter all which have no valid information

  // this is just for debug purpose and can be removed
mainDataArray.forEach(([path, filename]) => { 
    console.log(`path "${path}" / filename "${filename}"`)
  });

Using the above part, you can search inside the parsed array using Array.prototype.filter and assign the result to a list.

function test() {
  const input = document.getElementById('input').value,
    list = mainDataArray.filter(([path, filename]) => filename == input);
  if (list.length) {
    // show all results
    console.log(list.map(itm => itm.join(':')).join("\n"));
  } else {
    console.log("name not found");
  }
}

Show code snippet

c = [
  [
    ['dd\\32323232323:this1', 'dd\\43564564:this2'],
    ['dd\\5464656646:this3', 'dd\\43543453:this2']
  ]
]

var mainDataArray = c
  .flat(2) // flatten the array with depth of 3
  .map(itm => {
    try { // split each entry by :
      return itm.split(':');
    } catch(e) {
      console.error(e);
      return []
    }
  })
  .filter(arr => arr.length); // filter all which have no valid information

  // this is just for debug purpose and can be removed
mainDataArray.forEach(([path, filename]) => { 
  console.log(`path "${path}" / filename "${filename}"`)
  });

function test() {
  const input = document.getElementById('input').value,
    list = mainDataArray.filter(([path, filename]) => filename == input);
  if (list.length) {
    // show all results
    console.log(list.map(itm => itm.join(':')).join("\n"));
  } else {
    console.log("name not found");
  }
}
<input type="text" id="input">
<input type="button" onclick="test()" value="Search">
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related