How to extract items from a 2d array.
const array = [["1", "3"],["4", "3"]]
const itemToExclude = "3"
Use itemToExclude variable to exclude item from result (in this case 3)
Expected result is ["1", "4"]
const array = [["1", "3"],["4", "3"]]
const itemToExclude = "3"
const result = array.map((item) => {
return item.map((subitem) => {
return subitem
// exclude itemToExclude
})
})
console.log(result)
>Solution :
You can use Array flat and filter
const array = [
["1", "3"],
["4", "3"]
]
const itemToExclude = "3"
const result = array.flat().filter(item => item !== itemToExclude);
console.log(result)
You can also use reduce and inside the callback check if the element is not equals to itemToExclude
const array = [
["1", "3"],
["4", "3"]
]
const itemToExclude = "3"
const result = array.reduce((acc, curr) => {
for (let i of curr) {
if (i !== itemToExclude) {
acc.push(i)
}
}
return acc;
}, []);
console.log(result)