'how to get the first item in nested, filtered array in javascirpt
I've got an array:
const aContracts = [[ '0x8ae127d224094cb1b27e1b28a472e588cbcc7620', 0 ],
[ '0xcbf4ab00b6aa19b4d5d29c7c3508b393a1c01fe3', 0 ],
[ '0x03cd191f589d12b0582a99808cf19851e468e6b5', 0 ],
[ '0x0b3f868e0be5597d5db7feb59e1cadbb0fdda50a', 1 ]]
And I'm trying to filter using the second item in each sub-array. So I used:
const aGoodContracts = aContracts.filter(contracts => contracts[1]===1);
And I get:
[["0x0b3f868e0be5597d5db7feb59e1cadbb0fdda50a", 1]]
However, I want [EDIT I want an array of all the "good" contracts]:
"0x0b3f868e0be5597d5db7feb59e1cadbb0fdda50a"
So I tried a solution I found here:
const aGoodContracts = aContracts.map(k => k.filter(contracts => contracts[1]===1));
But I get:
[[], [], [], []]
What am I missing?
Solution 1:[1]
You can try this
const aContracts = [[ '0x8ae127d224094cb1b27e1b28a472e588cbcc7620', 0 ],
[ '0xcbf4ab00b6aa19b4d5d29c7c3508b393a1c01fe3', 0 ],
[ '0x03cd191f589d12b0582a99808cf19851e468e6b5', 0 ],
[ '0x0b3f868e0be5597d5db7feb59e1cadbb0fdda50a', 1 ]]
const aGoodContracts = aContracts.filter(contracts => contracts[1]===1)[0]
console.log(aGoodContracts[0])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad Raheel |
