JSFiddle - React, Tailwind, and code Playground

by jacobwsmith

JavaScript

console.clear();

function getKeyObj({
  site,
  placementId,
  creativeId
}) {
  return {
    placementId: placementId.toLowerCase(),
    site: site.toLowerCase(),
    key: site.toLowerCase() + placementId.toLowerCase() + creativeId.toLowerCase()
  }
}

function duplicateValidator(item, index, arr) {
  const currentKeyObj = getKeyObj(item);

  // compare current to array
  const isUnique = arr.every((res, i) => {
    const compareKeyObj = getKeyObj(res);
    if (i === index) {
      return true;
    }
    if (currentKeyObj.placementId !== compareKeyObj.placementId) {
      return true;
    }
    if (currentKeyObj.site === compareKeyObj.site && currentKeyObj.key !== compareKeyObj.key) {
      return true
    }
    return false;
  })

  return !isUnique;
}

{
  const testData = [{
    site: 'site1',
    placementId: 'pl1',
    creativeId: 'ad1'
  }, {
    site: 'site2',
    placementId: 'PL1',
    creativeId: 'ad'
  }];
  console.log(duplicateValidator(testData[0], 0, testData));
}

{
  const testData = [{
    site: 'site1',
    placementId: 'pl1',
    creativeId: 'ad1'
  }, {
    site: 'site1',
    placementId: 'PL1',
    creativeId: 'ad2'
  }];
  console.log(duplicateValidator(testData[0], 0, testData));
}