Reputation: 233
I have such array:
homeShapeShift: [
[
{text:"BTC", callback_data: 'btc'},
{text:"ETH", callback_data: 'eth'},
{text:"XRP", callback_data: 'xrp'},
{text:"ETC", callback_data: 'etc'},
],
[
{text:"ZEC", callback_data: 'zec'},
{text:"DASH", callback_data: 'dash'},
{text:"LTC", callback_data: 'ltc'},
{text:"OMG", callback_data: 'omg'},
],
[
{text:"ADA", callback_data: 'ada'},
{text:"BTG", callback_data: 'btg'},
{text:"TRX", callback_data: 'trx'},
{text:"NEO", callback_data: 'neo'},
],
]
How to remove object with text Zec and receive new array without it? I tried something with filter but didn't receive good result
let fe = keyboard.homeShapeShift.filter(k => k.filter(e => e.text !== 'ZEC'));
Upvotes: 8
Views: 28248
Reputation: 386530
You could filter the inner arrays by mapping them and then filter the outer array by the length of the inner arrays.
var homeShapeShift = [[{ text: "BTC", callback_data: 'btc' }, { text: "ETH", callback_data: 'eth' }, { text: "XRP", callback_data: 'xrp' }, { text: "ETC", callback_data: 'etc' }], [{ text: "ZEC", callback_data: 'zec' }, { text: "DASH", callback_data: 'dash' }, { text: "LTC", callback_data: 'ltc' }, { text: "OMG", callback_data: 'omg' }], [{ text: "ADA", callback_data: 'ada' }, { text: "BTG", callback_data: 'btg' }, { text: "TRX", callback_data: 'trx' }, { text: "NEO", callback_data: 'neo' }]],
result = homeShapeShift
.map(a => a.filter(({ text }) => text !== 'ZEC'))
.filter(({ length }) => length);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 3
Reputation: 138235
If you just want to remove one element just map the inner arrays to new inner filtered arrays:
let fe = keyboard.homeShapeShift.map(k => k.filter(e => e.text !== 'ZEC'));
Or if you wanna remove the whole array use every
to get a boolean:
let fe = keyboard.homeShapeShift.filter(k => k.every(e => e.text !== 'ZEC'));
that can be inversed too with some
:
let fe = keyboard.homeShapeShift.filter(k => !k.some(e => e.text === 'ZEC'));
Upvotes: 20