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?

nicael
  • 18,550
  • 13
  • 57
  • 90
Run
  • 54,938
  • 169
  • 450
  • 748
  • 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` – Pranav C Balan Jul 25 '16 at 07:33
  • You need two loops to match exactly. – Harry Bomrah Jul 25 '16 at 07:34
  • 1
    Do you need to compare all properties for match or even if 1 property is matching, message should be shown – Rajesh Jul 25 '16 at 07:36
  • 1
    You might like to look into something like [es5-shim](https://github.com/es-shims/es5-shim). It will give you access to lots of the new JavaScript features such as those used in the answers below. – David Gilbertson Jul 25 '16 at 08:26

3 Answers3

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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)
   }
});
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
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>
David Gilbertson
  • 4,219
  • 1
  • 26
  • 32
  • You should short-circuit loop on match. http://stackoverflow.com/questions/2641347/how-to-short-circuit-array-foreach-like-calling-break – Rajesh Jul 25 '16 at 07:51
  • 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. – David Gilbertson Jul 25 '16 at 07:55
  • for big arrays pre-hashing is much better – Variant Jul 25 '16 at 07:57