删除数组项列表

时间:2017-05-14 07:24:45

标签: javascript lodash

您能告诉我如何使用lodash删除数组项列表吗?我试过如下所示。但它没有用。

Video about the issue.

  _.remove(previousNumberArray, (a) => {

         _.some(this.removedQuestionCodes, (val, k) => {
            return a.questionCode == val.questionCode;
        });

   });

1 个答案:

答案 0 :(得分:1)

问题是括号{} 对于{}内的箭头功能应该有return

我们试试

  _.remove(previousNumberArray, (a) => _.some(this.removedQuestionCodes, (val, k) => {
       return a.questionCode == val;
  }));

  _.remove(previousNumberArray, (a) => {

         return _.some(this.removedQuestionCodes, (val, k) => {
            return a.questionCode == val;
        });

   });

让我们看看我的例子:

var fruits1 = ['Apple', 'Banana', 'Orange', 'Celery'];

_.remove(fruits1, fruit => {//BRACKET
  //MUST HAVE RETURN KEYWORD
  return _.indexOf(['Apple', 'Banana', 'Orange'], fruit) !== -1
});

var fruits2 = ['Apple', 'Banana', 'Orange', 'Celery'];
_.remove(fruits2, fruit => {//BRACKET
  //DONT HAVE RETURN KEYWORD
  _.indexOf(['Apple', 'Banana', 'Orange'], fruit) !== -1
});

console.log('fruits1',fruits1)


console.log('fruits2', fruits2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.6.1/lodash.js"></script>