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

Removing an item when using Array.prototype.map() in Javascript - Stack Overflow

programmeradmin1浏览0评论

I currently have a variable that contains an array of numbers

let numbersArray = [12, 15, 19, 20];

When looping over it I want to delete the entry if it is a certain value

let numbersArray = [12,15,19,20],
    filteredNumbersArray = numbersArray.map(function(value)
    {
        if(value === 15)
        {
            //delete the value
        } else {
            return value * 2;
        }
    });

In regards to using .filter(), the value must be mutatable, I have updated the example

I have searched through MDN but couldn't find anything about removing an element within a map().

The Rubber Duck didn't help either

I currently have a variable that contains an array of numbers

let numbersArray = [12, 15, 19, 20];

When looping over it I want to delete the entry if it is a certain value

let numbersArray = [12,15,19,20],
    filteredNumbersArray = numbersArray.map(function(value)
    {
        if(value === 15)
        {
            //delete the value
        } else {
            return value * 2;
        }
    });

In regards to using .filter(), the value must be mutatable, I have updated the example

I have searched through MDN but couldn't find anything about removing an element within a map().

The Rubber Duck didn't help either

Share Improve this question edited Apr 1, 2018 at 16:02 Ian asked Apr 1, 2018 at 15:58 IanIan 3,6764 gold badges30 silver badges50 bronze badges 3
  • 1 Array#filter can remove an item, Array#map returns a value for each item. – Nina Scholz Commented Apr 1, 2018 at 15:59
  • 1 What you need is filter and not map – Shubham Khatri Commented Apr 1, 2018 at 16:00
  • @NinaScholz Ah I see, it seems I missed a detail out of my question which I thought wasn't needed, I require value manipulation as well. – Ian Commented Apr 1, 2018 at 16:01
Add a comment  | 

3 Answers 3

Reset to default 9

You could filter and then map

var array = [12, 15, 19, 20],
    result = array
        .filter(v => v !== 15)
        .map(v => 2 * v);
        
console.log(result);

Or use the swiss knife of all array manipulations: Array#reduce

var array = [12, 15, 19, 20],
    result = array.reduce((r, v) => v !== 15 ? r.concat(v): r, []);
        
console.log(result);

With some optimizations

var array = [12, 15, 19, 20],
    result = array.reduce(function (r, v) {
        if (v !== 15) {
            r.push(2 * v);
        }
        return r;
    }, []);
        
console.log(result);

map can't remove values. If you need to simultaneously map and filter, your options are:

  1. filter, then map, e.g.:

    filteredNumbersArray = numbersArray.filter(function(v) { return v !== 15; })
                                       .map(function(v) { return v * 2; });
    

    or with ES2015+ syntax:

    filteredNumbersArray = numbersArray.filter(v => v !== 15)
                                       .map(v => v * 2);
    

    or

  2. Write your own thing, probably:

    filteredNumbersArray = [];
    numbersArray.forEach(function(v) {
        if (v !== 15) {
            filteredNumbersArray.push(v * 2);
        }
    });
    

    or with ES2015+ syntax:

    filteredNumbersArray = [];
    for (const v of numbersArray) {
        if (v !== 15) {
            filteredNumbersArray.push(v * 2);
        }
    }
    

    (You can also [ab]use reduce here, but it doesn't gain you anything worth having, and costs clarity.)

    If it comes up a lot for you, give yourself a combined map-and-filter function that uses some special return value to indicate that the entry should be dropped.

Map doesn't remove values it just mutates them. You could set the value to null but that is probably not your intent.

Filter might server you better
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

let numbersArray = [12,15,19,20],
filteredNumbersArray = numbersArray.filter(function(value)
{
    return value != 15
});

This would return an array without the number 15.

发布评论

评论列表(0)

  1. 暂无评论