0

I have 2 arrays X and Y:

X= [['a', 'b', 'c'], ['d', 'e', 'f']]

Y= [['a', 'x', 'c'], ['y', 'z', 'c']]

In reality X has hundreds of elements.

How can I know the INDEXES of X elements that have a and c in the 1st and 3rd position which are respectively Y[0][0] and Y[0][2] ?

I have tried combining methods findIndex(), toString() and indexOf() but I am not getting the result I expect.

It still gives results even if it finds SALE while I search SALES.

Cooper
  • 59,616
  • 6
  • 23
  • 54

3 Answers3

0

You can search like this. Here are sample code.

Y = [['a', 'x', 'c'], ['y', 'z', 'c']]
index = Y.toString().indexOf('y') / 2;
console.log( Math.floor(index / 3), index % 3)
// Another Method.
console.log(
  Math.floor(Y.toString().split(',').indexOf('y') / 3 ), 
  Y.toString().split(',').indexOf('y') % 3
)
Lion Simba
  • 21
  • 4
  • Hi, thanks. I am sorry I don't think I quiete understand your code. My goal is to find in X if it has elements (arrays) that have 'a' and 'c' at the first and third position (Y[0][0] and Y[0][2]). – Tojo Randrian Nov 14 '21 at 11:41
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 14 '21 at 11:43
0

Check this question for how to search array in array: javascript-search-array-of-arrays

basically you need to iterate through both arrays and check value by value if the element is in the second array (The code below it's just an example, it's not very optimal but you can start something from here)

const X = [
  ['a', 'b', 'c'],
  ['d', 'z', 'f']
]
const Y = [
  ['a', 'x', 'c'],
  ['y', 'z', 'c']
]

function indexOfArray(val, array) {
  var hash = {};
  for (var i = 0; i < array.length; i++) {
    hash[array[i]] = i;
  }
  return (hash.hasOwnProperty(val)) ? hash[val] : -1;
};

function findIndexInNestedArray(array1, array2) {
  array1.filter((xItem) => {
    console.log('X array ' + xItem)
    xItem.some((xValue) => {
      array2.filter((yItem) => {


        if (indexOfArray(xValue, yItem) !== -1)
          // you can access everything from here
          console.log('index of ', xValue, ' in', yItem, ' : ', indexOfArray(xValue, yItem))
          
          //return result as an object or whatever type you need
      })
    })
  });
}


findIndexInNestedArray(X, Y)
Mara Black
  • 1,666
  • 18
  • 23
  • Hi Mara, Thanks for replying. This is is the code I wrote: X= [['a', 'b', 'c'], ['d', 'e', 'f']] Y= [['a', 'x', 'c'], ['y', 'z', 'c']] var index = X.findIndex(function(item) {return item.toString().indexOf(Y[0][0]) != -1 && Y.toString().indexOf(X[0][2]) != -1}); if (index > -1){ 'do something ' } else {'do something else'} – Tojo Randrian Nov 14 '21 at 11:52
  • Are you sure about ypu want to hardcode the positions `X[0][0]` and `X[0][2]` ? The data can be different i guess.. if yes, your `00` and `02` will not work.. – Mara Black Nov 14 '21 at 12:28
0

This is the code I wrote:

X= [['a', 'b', 'c'], ['d', 'e', 'f']] 

Y= [['a', 'x', 'c'], ['y', 'z', 'c']]

var index = X.findIndex(function(item) {
    return item.toString().indexOf(Y[0][0]) != -1 && 
           Y.toString().indexOf(X[0][2]) != -1});

if (index > -1){ 
  //'do something' 
} else {
 //'do something else'
}
Mara Black
  • 1,666
  • 18
  • 23
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 14 '21 at 12:02