【问题标题】:Javascript: Using `.includes` to find if an array of objects contains a specific objectJavascript:使用`.includes`查找对象数组是否包含特定对象
【发布时间】:2018-03-09 06:39:23
【问题描述】:

我对 javascript ES6 有点陌生,我很难理解为什么以下内容没有按预期运行:

let check = [{name: 'trent'},{name: 'jason'}].includes({name: 'trent'}); 
// expect true - returns false

谢谢!

【问题讨论】:

    标签: javascript ecmascript-6


    【解决方案1】:

    includes 本质上检查是否有任何元素 === 是您正在搜索的元素。在对象的情况下,=== 的字面意思是同一个对象,就像在同一个引用中(内存中的同一个地方),而不是同一个形状。

    var a1 = { name: 'a' }
    var a2 = { name: 'a' }
    
    console.log(a1 === a2) // false because they are not the same object in memory even if they have the same data

    但如果你搜索一个实际在数组中的对象,它会起作用:

    var a1 = { name: 'a' }
    var a2 = { name: 'a' }
    var array = [a1, a2]
    
    console.log(array.includes(a1)) // true because the object pointed to by a1 is included in this array 

    【讨论】:

      【解决方案2】:

      它不起作用,因为对象永远不会相同,每个对象都有自己的引用:

      改用array.prototype.some

      const arr = [{name: 'trent'},{name: 'jason'}];
      const obj = {name: 'trent'}; 
      const check = arr.some(e => e.name === obj.name);
      console.log(check);

      【讨论】:

        【解决方案3】:

        其中之一

        let check = [{name: 'trent'}, {name: 'jason'}]
          .map(item => item.name)
          .includes('trent');
        

        let check = !![{name: 'trent'}, {name: 'jason'}].find(({name})=> name ==='trent')
        

        【讨论】:

          【解决方案4】:

          includes 检查值是否存在于数组中,您的情况是该值是参考值,并且对于每个声明的文字(即使文字相同)

          演示

          var a = {name: 'trent'};
          var b = {name: 'jason'};
          [a,b].includes(a);  //true
          

          使用some 代替匹配整个对象:

          var objToFind = JSON.stringify( {name: 'trent'} );
          let check = [{name: 'trent'},{name: 'jason'}].map( s => JSON.stringify( s ) ).some( s => s == objToFind ); 
          

          【讨论】:

            【解决方案5】:

            includes() 方法确定数组是否包含某个元素,并根据需要返回truefalse。但是在您比较两个对象的方式上,它们并不相等。它们应该在内存中具有相同的引用以彼此相等。

            您可以使用如下所示的内容

            var arr = [{name : "name1"}, {name : "name2"}];
            
            var objtoFind = {name : "name1"}
            var found = arr.find(function(element) {
              return element.name === objtoFind.name  ;
            });
            console.log((found ? true : false));

            【讨论】:

              【解决方案6】:

              检查数组是否包含x,y 对:

              这是我多次使用的Faly's answer 的实现。

              例如,如果您有一组 XY 坐标对,例如:

              var px=[{x:1,y:2},{x:2,y:3},{x:3,y:4}];
              

              ...您需要检查数组px 是否包含特定的XY 对,使用此函数:

              function includesXY(arr,x,y){return arr.some(e=>((e.x===x)&&(e.y===y)));}
              

              ...所以上面的px 数据集:

              console.log( includesXY( px, 2, 3) ); //returns TRUE
              console.log( includesXY( px, 3, 3) ); //returns FALSE
              

              var px=[{x:1,y:2},{x:2,y:3},{x:3,y:4}];
              
              console.log( includesXY( px, 2, 3) ); //returns TRUE
              console.log( includesXY( px, 3, 3) ); //returns FALSE
              
              function includesXY(a,x,y){return a.some(e=>((e.x===x)&&(e.y===y)));}

              【讨论】:

              • (但我忘记了如何让它可用 px.includesXY(x,y)... 任何人?!)
              【解决方案7】:

              您可以使用 Array.find() 方法检查数组是否包含对象,因为“Array.includes 检查数组中的 '==='”这对对象不起作用

              示例解决方案:

              let check = [{name: 'trent'},{name: 'jason'}].find(element => element.name === 'trent'); 
              

              【讨论】:

              • 比较对象时,会检查引用而不是内容 -> ===“也适用于”对象。
              猜你喜欢
              • 2020-06-07
              • 2021-11-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-01-12
              • 1970-01-01
              相关资源
              最近更新 更多