代码之家  ›  专栏  ›  技术社区  ›  Mukyuu

包含字符串和数字的两个数组之间的差异

  •  0
  • Mukyuu  · 技术社区  · 6 年前

    我能够找到一个数组和另一个数组之间的区别(参见 a c )但问题是,如果数组在一个数组中包含一个数字。我试着做以下工作: b.forEach(el=>{el.toString();}); a.forEach(el=>{parseInt(el, 10);}); 无济于事。

    var a= ["473", "204"];
    var b= [473, 204]; //problem is if number with string
    var c= ["473", "204"];
    
    console.log(a.filter(function (n) { return !this.has(n); }, new Set(b)));
    console.log(a.filter(function (n) { return !this.has(n); }, new Set(c)));
    
    b.forEach(el=>{el.toString();});
    console.log(a.filter(function (n) { return !this.has(n); }, new Set(b)));
    
    a.forEach(el=>{parseInt(el, 10);});
    console.log(a.filter(function (n) { return !this.has(n); }, new Set(b)));

    我想问一下,是否有一种方法可以区分两个数组,其中包含具有“相同”值的不同类型?

    背景:检索到的数组之一是元素 id 另一个来自查询。

    5 回复  |  直到 6 年前
        1
  •  2
  •   Nick Parsons Felix Kling    6 年前

    你可以 map 你的阵列在你的 Set 成为数字,然后当你使用 has 方法可以使用 unary + operator :

    var a= ["473", "204"];
    var b= [473, 204]; //problem is if number with string
    var c= ["473", "204"];
    
    var resB = a.filter(function (n) { return !this.has(+n); }, new Set(b.map(Number)));
    var resC = a.filter(function (n) { return !this.has(+n); }, new Set(c.map(Number)));
    
    console.log(resB);
    console.log(resC);
        2
  •  1
  •   Snow    6 年前

    你的

    b.forEach(el=>{el.toString();});
    

    a.forEach(el=>{parseInt(el, 10);});
    

    根本不做任何事情-您正在创建一个具有该项的字符串或数字表示形式的表达式,然后再对该表达式不做任何事情。原始数组保持不变。

    要更改原始数组,必须显式地分配给它,例如

    a.forEach((el, i) => {
      a[i] = parseInt(el, 10)
    

    或者,更好的是,使用 Array.prototype.map :

    var a= ["473", "204"];
    var b= [473, 204]; //problem is if number with string
    var c= ["473", "204"];
    
    const aNums = a.map(Number);
    console.log(aNums.filter(function (n) { return !this.has(n); }, new Set(b)));
        3
  •  1
  •   last_fix    6 年前

    使用类型

    console.log(typeof 42);
    // expected output: "number"
    
    console.log(typeof 'blubber');
    // expected output: "string"
    
    console.log(typeof true);
    // expected output: "boolean"
    
    console.log(typeof declaredButUndefinedVariable);
    // expected output: "undefined";
        4
  •  1
  •   Shidersz    6 年前

    另一种可能的解决方案是检查集合是否不包含 n 作为一个数字( +n n 作为一个字符串( n.toString() ):

    var a = ["473", "204"];
    var b = [473, 204]; //problem is if number with string
    var c = ["473", "204"];
    var d = [473, "199", 175];
    
    console.log(
       a.filter(function(n)
       {
           return !(this.has(+n) || this.has(n.toString()));
       }, new Set(b))
    );
    console.log(
        a.filter(function(n)
        {
           return !(this.has(+n) || this.has(n.toString()));
        }, new Set(c))
    );
    console.log(
        a.filter(function(n)
        {
           return !(this.has(+n) || this.has(n.toString()));
        }, new Set(d))
    );
    .as-console {background-color:black !important; color:lime;}

    不使用 Set ,你可以给 Array.some() 以及 == 比较。

    var a = ["473", "204"];
    var b = [473, 204]; //problem is if number with string
    var c = ["473", "204"];
    var d = [473, "199", 175];
    
    console.log(a.filter(n => !b.some(x => x == n)));
    console.log(a.filter(n => !c.some(x => x == n)));
    console.log(a.filter(n => !d.some(x => x == n)));
    .作为控制台背景色:黑色!重要;颜色:石灰;
        5
  •  0
  •   automasean    6 年前

    可以使用double equals(又称抽象相等比较)运算符进行类型强制:

    console.log(473 == '473') // true
    

    MDN reference on JS equality comparisons