8

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'));
2
  • What's your desired output, exactly? Commented Aug 4, 2018 at 21:02
  • Just change your first filter to map and you should be good: keyboard.homeShapeShift.map(k => k.filter(e => e.text !== 'ZEC')); Commented Aug 4, 2018 at 21:40

2 Answers 2

20

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'));
Sign up to request clarification or add additional context in comments.

Comments

3

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.