I have a three lists, the first one is like this
[ true, ['keyToOffice', false], false ]
The second one is like this:
[ { reference: 'keyToOffice', a load of other irrelevant stuff }, { reference: 'keyToHouse', a load of other irrelevant stuff } ]
The third one, my inventory is like this:
[ 'keyToOffice' ]
I would like to know if there is a way to update the second value of all the lists in the first list to true if the first item is inside of the third list.
What would happen is that in ['keyToOffice', false]
, the false
would become true because it is in the inventory list. If 'keyToOffice'
was not in the inventory, then the false
would stay false
Advertisement
Answer
If I understand your problem correctly, you don’t need the second list. You can just iterate through the first list, and if the element is an array, and the string in it is included in the third list, change the boolean to true.
What map does is iterates through an array, and returns a one to one representation of that array (same number of elements), with certain elements changed according to the rules inside the function.
const firstList = [ true, ['keyToOffice', false], false, true, ['keyToHouse', false], false, true, ['keyToCar', false], false, ] const thirdList = [ 'keyToOffice', 'keyToCar' ] const firstListAmended = firstList.map(el => { if(Array.isArray(el)) { if(thirdList.includes(el[0])) { el[1] = true } } return el }) console.log(firstListAmended)