1

How can I find there is a match between an array and a json?

var fields = [ { public_name: 'hello', code_name: 'world' },
  { public_name: 'particles', code_name: 'pm2.5' },
  { public_name: 'humidity', code_name: 'hum' } ]

var species = [{"code_name":"hum","public_name":"humidity"},
  {"code_name":"pm2.5","public_name":"particles"},
  {"code_name":"world","public_name":"hello"}]


fields.forEach(function(field, index) {       
    if (field.code_name === species.code_name) {
        console.log('match:' + code_name)
    }
});

The result should be:

match: world
match: pm2.5
match: hum

Any ideas?

4
  • 2
    if (field.code_name === species[index].code_name) { console.log('match:' + field.code_name) } you need to iterate and compare with each value in species, use Array#some Commented Jul 25, 2016 at 7:33
  • You need two loops to match exactly. Commented Jul 25, 2016 at 7:34
  • 1
    Do you need to compare all properties for match or even if 1 property is matching, message should be shown Commented Jul 25, 2016 at 7:36
  • 1
    You might like to look into something like es5-shim. It will give you access to lots of the new JavaScript features such as those used in the answers below. Commented Jul 25, 2016 at 8:26

3 Answers 3

3

You could use a hash table for lookup.

var fields = [{ public_name: 'hello', code_name: 'world' }, { public_name: 'particles', code_name: 'pm2.5' }, { public_name: 'humidity', code_name: 'hum' }],
    species = [{ "code_name": "hum", "public_name": "humidity" }, { "code_name": "pm2.5", "public_name": "particles" }, { "code_name": "world", "public_name": "hello" }],
    match,
    hash = Object.create(null),
    getHashKey = function (o) { return o.public_name + '|' + o.code_name; };

fields.forEach(function (a) {
    hash[getHashKey(a)] = true;
});

match = fields.filter(function (a) {
    return hash[getHashKey(a)];
});

console.log(match);

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it by using find() at this context,

fields.forEach(function(field, index) {       
   if(species.find((itm) => itm.code_name == field.code_name)) {
      console.log("match" , " : " , field.code_name)
   }
});

Or the better option would be using some() since it will return the result in Boolean's,

fields.forEach(function(field, index) {       
   if(species.some((itm) => itm.code_name == field.code_name)) {
      console.log("match" , " : " , field.code_name)
   }
});

4 Comments

Though I was going to do the same, do specify that Array.find has compatibility issues
@PranavCBalan Yeah, That would yield true/false unlike a find. find would return the matched element if any.
@Rajesh But eventually, browsers will start support ES6 shortly. So we have to start writing code in that. :)
@RajaprabhuAravindasamy but what about browsers like IE8 or IE9? At least you should mention it and if OP wants to use it for older browsers, he/she can add polyfill.
1

If you can't use newer JS features, you can just nest two loops like so.

var fields = [
  { public_name: 'hello', code_name: 'world' },
  { public_name: 'hello', code_name: 'NO MATCH' },
  { public_name: 'particles', code_name: 'pm2.5' },
  { public_name: 'humidity', code_name: 'hum' }
];

var species = [
  {"code_name":"hum","public_name":"humidity"},
  {"code_name":"pm2.5","public_name":"particles"},
  {"code_name":"world","public_name":"hello"}
];

var matches = [];

fields.forEach(function(field, index) {
  species.forEach(function(spec) {
    if (spec.code_name === field.code_name) {
      matches.push(field.code_name);
    }
  });
});

document.getElementById('result').textContent = matches.join(', ');
<div id="result"></div>

3 Comments

You should short-circuit loop on match. stackoverflow.com/questions/2641347/…
Agreed, in theory. @teelou, if either array could grow bigger than a few thousand entries, you should consider performance and not continue looping after you have found a match.
for big arrays pre-hashing is much better

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.