简体   繁体   English

如何过滤数组

[英]How to filter array of arrays

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? 如何用文本Zec删除对象并接收没有它的新数组? I tried something with filter but didn't receive good result 我尝试使用过滤器进行操作,但未收到良好结果

let fe = keyboard.homeShapeShift.filter(k => k.filter(e => e.text !== 'ZEC'));

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: 或者,如果您想删除整个数组,请使用every获取布尔值:

 let fe = keyboard.homeShapeShift.filter(k => k.every(e => e.text !== 'ZEC'));

that can be inversed too with some : 也可以用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; } 

var  fe = [];
var obj = [];

homeShapeShift.forEach(function(element) {
    element.forEach(function(element) {
        if(element.text !== 'ZEC'){
            obj.push(element);
        }
    });
    fe.push(obj);
    obj = [];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM