【问题标题】:Remove all elements from the initial array that are of the same value as the arguments followed by the initial array从初始数组中删除与初始数组后跟参数具有相同值的所有元素
【发布时间】:2016-01-08 12:09:51
【问题描述】:

有一个初始数组(销毁函数中的第一个参数),后跟一个或多个参数。从初始数组中删除与这些参数具有相同值的所有元素。

这是我的代码,但我无法解决问题。

function destroyer(arr) {
// First I have converted the whole input into an array including the arguments
var args = Array.prototype.slice.call(arguments);
var abc=args.splice(0,1);//Here I have taken out the array part i.e[1,2,3,1,2,3] and also now args contain 2,3 only

function des(abc){
           for(var i=0;i<abc.length;i++){//I tried to check it for whole length of abc array
                if(args.indexOf(abc)===-1){
   return true;   //This should return elements of [1,2,3,1,2,3] which are not equal to 2,3 i.e [1,1] but for string inputs it is working correctly.How to do for these numbers?
     }
  }
}
return arr.filter(des); //but this filter function is returning  empty.How to solve my problem??
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

对于destroyer(["tree", "hamburger", 53], "tree", 53),代码给出输出["hamburger"],工作正常。

但是对于驱逐舰([1, 2, 3, 1, 2, 3], 2, 3);代码没有输出。

【问题讨论】:

标签: javascript arguments indexof destroy splice


【解决方案1】:

您可以使用Array.filter。以下示例描述了相同的

同样destroyer([1, 2, 3, 1, 2, 3], 2, 3); 在此调用中,[1, 2, 3, 1, 2, 3] 是第一个参数,2 是第二个参数,3 是第三个参数。所以 arr 将是 [1, 2, 3, 1, 2, 3] 而不是 [1, 2, 3, 1, 2, 3], 2, 3

function removeElementFromArray(arr,num){
  var _temp = arr.filter(function(item){
      return (item !== num);
  });
  
  return _temp;
}

(function main(){
  var arr = [1,1,1,2,4,3,2,4,5,4,3,1,4,5,2];
  var result = removeElementFromArray(arr,1);
  var result1 = removeElementFromArray(arr,3);
  
  console.log(result, result1);
})()

【讨论】:

  • 嗯 ...if (item !== num) return true;return item !== num;
【解决方案2】:

试试这个:

function destroyer(arr) {
    var args = Array.prototype.slice.call(arguments, 1);

    function des(abc){
        return args.indexOf(abc) === -1;
    }
    return arr.filter(des);
}
var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
document.getElementById('output').innerHTML = JSON.stringify(result);
&lt;pre id="output"&gt;&lt;/pre&gt;

【讨论】:

  • 迟到的评论...但是为什么函数 des(abc) 有效?为什么'abc'我没有看到它在其他地方声明?
  • @anatp_123 abc 是函数参数,可以是xnumber
【解决方案3】:

Lodash 没有可用的方法。

link to doc.

来自 lodash 文档:

_.without(数组,[值])

使用 SameValueZero 创建一个排除所有提供值的数组 相等比较。

示例

_.without([1, 2, 1, 3], 1, 2);
// → [3]

【讨论】:

    【解决方案4】:

    这应该适合你:

    function destroyer(arr) {
        var args = Array.prototype.slice.call(arguments);
        var abc = args.splice(0, 1);
        function des(value) {
            // you don't need loop here, just check if given value is in args array
            return args.indexOf(value) === -1;
        }
        // splice returns array of deleted elements so that your first argument is abc[0]
        return abc[0].filter(des);
    }
    var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
    console.log(result);
    

    【讨论】:

    • function destroyer(arr) { // Remove all the values var args = Array.prototype.slice.call(arguments); var abc=args.splice(0,1); function des(abc){ return args.indexOf(abc)===-1; } return arr.filter(des); } destroyer([1, 2, 3, 1, 2, 3], 2, 3);
    • 非常感谢您的帮助,我像这样更改了我的代码,但我认为 abc[0] 没有必要 arr 就足够了。我运行了所有测试用例。谢谢。
    • 不客气!只是不要使用abc 作为过滤器函数的参数名称。
    【解决方案5】:

    试试这个简单的代码:

    function destroyer(arr) {
      /* Put all arguments in an array using spread operator and remove elements 
         starting from 1 */
      const args = [...arguments].splice(1);
      /* Check whether arguments include elements from an array and return all that 
         do not include(false) */
      return arr.filter(el => !args.includes(el));
    }
    
    destroyer([1, 2, 3, 1, 2, 3], 2, 3); // [1, 1]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2020-09-29
      • 2015-05-31
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多