Ricky
Ricky

Reputation: 153

How to check if there is the same string in an object?

Using JavaScript/Node I'm trying to have a function check if a string (x) is in any one of the objects in an array. Here is an example:

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

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


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

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

I want to see check if any of arr2's properties are inside of any of the properties within the arr 1 objects, and simply log to the console if a property isn't the same. Sorrry if this is confusing, I'm new to JavaScript and it's hard to explain :(

I've tried this:

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

I've also tried looping over both arrays and comparing, but I can't pick out the ones that are different.

Upvotes: 0

Views: 241

Answers (3)

Alok Deshwal
Alok Deshwal

Reputation: 1126

Solution goes here:

    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);

Upvotes: 1

marzelin
marzelin

Reputation: 11600

Create an object from arr1 that will store all prop:values from the array since all props in arr2 will need to traverse the whole props:

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

For each prop in arr2 check it against aggregatedArr1 props and if match print the value of the prop:

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();

Upvotes: 0

Maheer Ali
Maheer Ali

Reputation: 36564

use forEach on the arr1 and inside it use for..in. Your values in objects shows you wanna check only same index objects

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])
  }
})

Upvotes: 0

Related Questions