Home > database >  In JavaScript How to check array values exists in array of object without duplication
In JavaScript How to check array values exists in array of object without duplication

Time:10-08

I have an array

let names = ['Devid', 'Aries','James'];

and an array of object

let details = [
{"Name":"John"},
{"Name":"Devid"},
{"Name":"Aries"},
{"Name":"Aries"}
];

I want to check all names exist in detail also they are not duplicate by name in 1 loop If duplicate values exist then count it as 1 Output should be count= 2 but my code is returning count 3

 let names = ['Devid', 'Aries','James'];

 let details = [
    {"Name":"John"},
    {"Name":"Devid"},
    {"Name":"Aries"},
    {"Name":"Aries"}
    ];

let count = 0;

for (var eachDetail of details) {
     // checking here Name exist
     let isNameFound = names.some(el => eachDetail.Name.includes(el));
            if (isNameFound) count  ;
        }
console.log(count);

// console.log(3) expected output 2

Output Should be 2 (As Devid and Aries) exist

CodePudding user response:

You could simply iterate over the names array and perform a details.find(...) operation. If you find the object in details, add it to the resulting list.

let names = ['Devid', 'Aries','James'];

let details = [
  {"Name":"John"},
  {"Name":"Devid"},
  {"Name":"Aries"},
  {"Name":"Aries"}
];

let result = [];
names.forEach(name => {
  const object = details.find(detail => detail.Name === name);
  if (object) {
    result.push(object);
  }
});

console.log(result.length   ' entries found:');
console.log(JSON.stringify(result, null, 2));

CodePudding user response:

You can use reduce to count the elements in the array that can be found in the object's values. You can use a Set to remove duplicates.

let names = ['Devid', 'Aries','James'];

let details = [
  {"Name":"John"},
  {"Name":"Devid"},
  {"Name":"Aries"},
  {"Name":"Aries"}
];

const detailsNames = [...new Set(details.map(d => d.Name))];
const count = [...new Set(names)].reduce((acc, el) => acc   detailsNames.includes(el), 0);

console.log(count);

CodePudding user response:

You should be able to filter the array using .filter(), with also a .some() and .inlcude() methods inside verifying nonduplicates.

Please see documentation on JS Array methods here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

let names = ['Devid', 'Aries','James'];
let tempNames = [];

let details = [
  {"Name":"John"},
  {"Name":"Devid"},
  {"Name":"Aries"},
  {"Name":"Aries"}
];

var newDetails = details.filter(x => {
    let includeName = names.some(j => j == x.Name) && !tempNames.includes(x.Name);
    tempNames.push(x.Name);
    return includeName;
});

console.log(newDetails);

CodePudding user response:

You can prototype Array, then loop through names array

Array.prototype._indexOfName = function(name){
  let n = 0;
  for(; n < this.length; n  )
    if(this[n].Name == name)
      return n
  return -1
};

let names = ['Devid', 'Aries','James'];
let details = [
  {"Name":"John","Age":20},
  {"Name":"Devid","Age":23},
  {"Name":"Aries","Age":34},
  {"Name":"Aries","Age":50}
];
let n = 0;
let n2 = 0;
for(; n < names.length; n  )
  if(details._indexOfName(names[n]) > -1)
    n2   
alert('Found '   n2   ' matches')

  • Related