dim_dev
dim_dev

Reputation: 359

How to find data in an array of arrays?

I have an array of arrays, I know it is not practical but the component I use renders (using React.js) data only as an array of arrays I delete some element of this array in a backend (Firestore) and I need to delete the data from this array on frontend. The array:

const myData = [
  ["2020-09-14", "15:00", "60", "Info", "April Tucker", "Other", "no"],
  ["2020-09-14", "2:00", "50", "Text", "April Tucker", "Other", "yes"]
]

When I delete the data in backend I get the data I deleted as a change.doc.data(). Then I am trying to find the index of the deleted data in the myData state.

  if (change.type === 'removed') {
          console.log('Removed data: ', change.doc.data())
          const index = myData.indexOf(
            (element) =>
              element[0] == change.doc.data()[0] &&
              element[1] == change.doc.data()[1] &&
              element[2] == change.doc.data()[2] &&
              element[3] == change.doc.data()[3] &&
              element[4] == change.doc.data()[4] &&
              element[5] == change.doc.data()[5] &&
              element[6] == change.doc.data()[6]
          )
          console.log(index)
          mydata.splice(index, 1)
       
        }

This doesn't work, returning the index -1. I also tried mapping over the myData array, but my approach was not successful.

Upvotes: 0

Views: 184

Answers (1)

JavaMan
JavaMan

Reputation: 1217

I would suggest you take a look at this site

Especially at the following method which should get you started:

function arrayAlreadyHasArray(arr, subarr){
    for(var i = 0; i<arr.length; i++){
        let checker = false
        for(var j = 0; j<arr[i].length; j++){
            if(arr[i][j] === subarr[j]){
                checker = true
            } else {
                checker = false
                break;
            }
        }
        if (checker){
            return true
        }
    }
    return false
}

Upvotes: 2

Related Questions