最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to filter array of arrays - Stack Overflow

programmeradmin0浏览0评论

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'));

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'));
Share Improve this question asked Aug 4, 2018 at 21:00 Erfercfsfr ErferfErfercfsfr Erferf 2331 gold badge3 silver badges8 bronze badges 2
  • What's your desired output, exactly? – CertainPerformance 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')); – Akrion Commented Aug 4, 2018 at 21:40
Add a comment  | 

2 Answers 2

Reset to default 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'));

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

发布评论

评论列表(0)

  1. 暂无评论