0

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.

3
  • 1
    Should we compare the same index of arr1 and arr2? Like x: 'testx' should be checked only on arr[0]? Commented Feb 10, 2019 at 6:46
  • 1
    Please give us your expected output Commented Feb 10, 2019 at 6:47
  • is this your own data structure or is it given to you? Because if it's your own data structure, you might want to reconsider its structure. Commented Feb 10, 2019 at 6:48

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

this worked perfect! I'll do some more research on keys in objects tonight, thank you!!!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.