Removing elements with Array.map in JavaScript

后端 未结 8 2057
无人及你
无人及你 2020-12-08 12:43

I would like to filter an array of items by using the map() function. Here is a code snippet:

var filteredItems = items.map(function(item)
{
            


        
相关标签:
8条回答
  • 2020-12-08 13:06

    First you can use map and with chaining you can use filter

    state.map(item => {
                if(item.id === action.item.id){   
                        return {
                            id : action.item.id,
                            name : item.name,
                            price: item.price,
                            quantity : item.quantity-1
                        }
    
                }else{
                    return item;
                }
            }).filter(item => {
                if(item.quantity <= 0){
                    return false;
                }else{
                    return true;
                }
            });
    
    0 讨论(0)
  • 2020-12-08 13:07

    You should use the filter method rather than map unless you want to mutate the items in the array, in addition to filtering.

    eg.

    var filteredItems = items.filter(function(item)
    {
        return ...some condition...;
    });
    

    [Edit: Of course you could always do sourceArray.filter(...).map(...) to both filter and mutate]

    0 讨论(0)
  • 2020-12-08 13:07

    I just wrote array intersection that correctly handles also duplicates

    https://gist.github.com/gkucmierz/8ee04544fa842411f7553ef66ac2fcf0

    // array intersection that correctly handles also duplicates
    
    const intersection = (a1, a2) => {
      const cnt = new Map();
      a2.map(el => cnt[el] = el in cnt ? cnt[el] + 1 : 1);
      return a1.filter(el => el in cnt && 0 < cnt[el]--);
    };
    
    const l = console.log;
    l(intersection('1234'.split``, '3456'.split``)); // [ '3', '4' ]
    l(intersection('12344'.split``, '3456'.split``)); // [ '3', '4' ]
    l(intersection('1234'.split``, '33456'.split``)); // [ '3', '4' ]
    l(intersection('12334'.split``, '33456'.split``)); // [ '3', '3', '4' ]

    0 讨论(0)
  • 2020-12-08 13:08

    That's not what map does. You really want Array.filter. Or if you really want to remove the elements from the original list, you're going to need to do it imperatively with a for loop.

    0 讨论(0)
  • 2020-12-08 13:14

    following statement cleans object using map function.

    var arraytoclean = [{v:65, toberemoved:"gronf"}, {v:12, toberemoved:null}, {v:4}];
    arraytoclean.map((x,i)=>x.toberemoved=undefined);
    console.dir(arraytoclean);
    
    0 讨论(0)
  • 2020-12-08 13:17

    You must note however that the Array.filter is not supported in all browser so, you must to prototyped:

    //This prototype is provided by the Mozilla foundation and
    //is distributed under the MIT license.
    //http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
    
    if (!Array.prototype.filter)
    {
        Array.prototype.filter = function(fun /*, thisp*/)
        {
            var len = this.length;
    
            if (typeof fun != "function")
                throw new TypeError();
    
            var res = new Array();
            var thisp = arguments[1];
    
            for (var i = 0; i < len; i++)
            {
                if (i in this)
                {
                    var val = this[i]; // in case fun mutates this
    
                    if (fun.call(thisp, val, i, this))
                       res.push(val);
                }
            }
    
            return res;
        };
    }
    

    And doing so, you can prototype any method you may need.

    0 讨论(0)
提交回复
热议问题