【问题标题】:How to check if there is the same string in an object?如何检查对象中是否存在相同的字符串?
【发布时间】:2019-02-10 06:34:59
【问题描述】:

使用 JavaScript/节点 我试图让一个函数检查字符串(x)是否在数组中的任何一个对象中。这是一个例子:

{
x: 'testx',
y: 'testy',
z: 'textz'
},

{
a: 'testa',
b: 'testb',
c: 'textc'
}
]


var arr2 = [
{
x: 'testx'
},

{
a: 'testa',
b: 'notb'
}
]

我想检查 arr2 的任何属性是否在 arr 1 对象内的任何属性内,如果属性不一样,只需登录到控制台即可。抱歉,如果这令人困惑,我是 JavaScript 新手,很难解释 :(

我试过这个:

newProducts.each((i, el) => {
        if (Object.values(arr).indexOf('testb') > -1) {
            console.log(`has testb`);
        }
    });

我也尝试过遍历两个数组并进行比较,但我无法挑选出不同的数组。

【问题讨论】:

  • 我们是否应该比较arr1arr2的相同索引?像x: 'testx' 应该只在arr[0] 上检查?
  • 请给我们您的预期输出
  • 这是你自己的数据结构还是给你的?因为如果它是您自己的数据结构,您可能需要重新考虑它的结构。

标签: javascript string object


【解决方案1】:

解决方案在这里:

    var arr1 = [{
        x: 'testx',
        y: 'testy',
        z: 'textz'
    },
    {
        a: 'testa',
        b: 'testb',
        c: 'textc'
    }];
var arr2 = [
    {
        x: 'testx'
    },
    {
        a: 'testa',
        b: 'notb'
    }
]
var set = {};
arr1.forEach((elem, index, arr)=> {
    for(let key in elem) {
        set[key] = elem[key];
    }
});
let matches = {};
arr2.forEach((elem, index, arr) => {
    for(let key in elem) {
        if(set[key] === elem[key]) {
            matches[key] = set[key];
        }
    }
})
console.log(matches);

【讨论】:

    【解决方案2】:

    arr1 上使用forEach,在其中使用for..in。您在对象中的值表明您只想检查相同的索引对象

    var arr1 =  [{
        x: 'testx',
        y: 'testy',
        z: 'textz'
      },
    
      {
        a: 'testa',
        b: 'testb',
        c: 'textc'
      }
    ]
    
    
    var arr2 = [
      {
       x: 'testx'
      },
    
      {
       a: 'testa',
       b: 'notb'
      }
    ]
    arr1.forEach((obj,i) => {
      for(let key in obj){
        if(obj[key] === arr2[i][key]) console.log(obj[key])
      }
    })

    【讨论】:

    • 这很完美!今晚我会对对象中的键进行更多研究,谢谢!!!
    【解决方案3】:

    arr1 创建一个对象,该对象将存储数组中的所有 prop:values,因为 arr2 中的所有 props 都需要遍历整个 props:

    const aggregatedArr1 = arr1.reduce((acc, o) => (Object.keys(o).forEach(k => acc[k] = o[k]), acc), {});
    

    对于arr2 中的每个道具,对照aggregatedArr1 道具检查它,如果匹配则打印道具的值:

    arr2.forEach(obj => Object.entries(obj).forEach(([key, val]) => {
        if (aggregatedArr1.hasOwnProperty(key) && aggregatedArr1[key] !== val) {
          console.log(val);
        }
      }))
    

    function main() {
      const aggregatedArr1 = arr1.reduce((acc, o) => (Object.keys(o).forEach(k => acc[k] = o[k]), acc), {});
      arr2.forEach(obj => Object.entries(obj).forEach(([key, val]) => {
        if (aggregatedArr1.hasOwnProperty(key) && aggregatedArr1[key] !== val) {
          console.log(val);
        }
      }))
    }
    
    var arr1 = [{
        x: 'testx',
        y: 'testy',
        z: 'textz'
      },
    
      {
        a: 'testa',
        b: 'testb',
        c: 'textc'
      }
    ]
    
    
    var arr2 = [{
        x: 'testx'
      },
    
      {
        a: 'testa',
        b: 'notb'
      }
    ]
    
    main();

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2021-11-20
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2014-09-10
      • 2019-07-06
      相关资源
      最近更新 更多