0

My purpose is comparison data and combine .
When my data is similar combine must return something.

var data = {"Q1":"Male","Q5":"USA","Q3":"BBB"};  
var combine = [{"Q1":"Male","Q5":"USA"},{"Q1":"Male","Q5":"Japan"}];

var num = combine.length;
for (var i = 0; i < num; i++) {
  var Keys = Object.keys(combine[i]);
  Keys.forEach(function(element) {
    if (element.length > 1) {

      console.log(element, data[element], combine[i][element]);

      if ((data[element] == combine[i][element])) {
        console.log("same");
      }
    }
  });
}

And you will see the data has 3 and combine has 2 because i
just want to compare just Q1, Q5 (remake in var Keys = Object.keys(combine[i]); )
or you has another ways to help please tell me.

more example

enter image description here

adiga
  • 34,372
  • 9
  • 61
  • 83
BBBBeloved
  • 75
  • 1
  • 1
  • 7

1 Answers1

0

You can use a combination of some and every to check if at least one item in combine which has all the properties in it same as data

var data = {"Q1":"Male","Q5":"USA","Q3":"BBB"}; 
    
var combine = [{"Q1":"Male","Q5":"USA"},{"Q1":"Male","Q5":"Japan"}];

var exists = combine.some(function(c){
  const keys = Object.keys(c);
  return keys.every(key => c[key] === data[key])
});

console.log(exists ? "same" : "not same")

If you want to get the first item from combine which matches the condition, then use find instead of some

var data = {"Q1":"Male","Q5":"USA","Q3":"BBB"}; 
    
var combine = [{"Q1":"Male","Q5":"USA"},{"Q1":"Male","Q5":"Japan"}];

var match = combine.find(function(c){
  const keys = Object.keys(c);
  return keys.every(key => c[key] === data[key])
});

console.log(match)
adiga
  • 34,372
  • 9
  • 61
  • 83
  • But my service not always be Q1 and Q5. That's why i try to use var Keys = Object.keys(combine[i]); and forEach – BBBBeloved Jan 30 '19 at 09:32
  • @BBBBeloved now it checks for every key in `combine[i]` – adiga Jan 30 '19 at 09:36
  • If i need the JSON of combine[0](combine position has same with data is possible?) and thank for your help – BBBBeloved Jan 30 '19 at 09:48
  • @BBBBeloved then you don't need the `some` wrapper. Just: `const keys = Object.keys(combine[0]); var exists = keys.every(key => combine[0][key] === data[key])` – adiga Jan 30 '19 at 09:54
  • Where I must edit? Can you edit in your Code please? – BBBBeloved Jan 30 '19 at 10:03
  • @BBBBeloved remove the entire `some` code. Everything except `console.log` – adiga Jan 30 '19 at 10:09
  • They said : combine is not a function. var exists = combine(function(c){ ... }); – BBBBeloved Jan 30 '19 at 10:15
  • @BBBBeloved When did i ever mention to use `combine` as a function? If you want to check just for first item in combine: `var data= {...}; var combine = [...]; var keys = Object.keys(combine[0]); var exists = keys.every(key => combine[0][key] === data[key]); console.log(exists ? "same" : "not same")`. That's it. – adiga Jan 30 '19 at 10:17
  • No i mean whatever position is same return JSON data look like this {"Q1":"Male","Q5":"USA"} if not same at all return false or not same... I'm sorry if i make you get wrong. – BBBBeloved Jan 30 '19 at 10:21
  • @BBBBeloved if there is a match, you want to get the matched item from `combine`? – adiga Jan 30 '19 at 10:22
  • Yes if there is a match. i need JSON data match from combine too. – BBBBeloved Jan 30 '19 at 10:23
  • @BBBBeloved you want *all the matches* in an array or just the *first matched object*? – adiga Jan 30 '19 at 10:24
  • Just one (only item match in combine).I mean just first matches with data – BBBBeloved Jan 30 '19 at 10:27
  • @BBBBeloved updated the answer. `some` returns a boolean. Just checks if it exists or not. `find` returns the first item which matches the condition. `filter` returns multiple items which match a condition – adiga Jan 30 '19 at 10:32
  • Your are be kind thank for your help. I find the way so long. – BBBBeloved Jan 30 '19 at 10:36
  • @BBBBeloved you're welcome. You can mark the answer as accepted if it resolved your issue. If you had mentioned your expected output as `{"Q1":"Male","Q5":"USA"}` in the question itself, this would have been resolved in 5 minutes. Please add the expected output to question next time :) – adiga Jan 30 '19 at 10:38