Skip to content Skip to sidebar Skip to footer

Searching For Unique Properties In The Array Object

var name = [ { firstN: 'Dave', lastN: 'Mike', fullName: 'Dave Mike }, { firstN: 'Dave', lastN: 'Pence', fullName: 'Dave Pence' }, { firstN: 'Tom', lastN: 'Pence

Solution 1:

array.map is the wrong method to use since it's a 1-to-1 array transformation. What you're looking for is a method that can trim down an array, either array.filter or array.reduce and a temporary key-value store that records names you've already encountered.

const names = [{
    firstN: 'Dave',
    lastN: 'Mike',
    fullName: 'Dave Mike'
  },
  {
    firstN: 'Dave',
    lastN: 'Pence',
    fullName: 'Dave Pence'
  },
  {
    firstN: 'Tom',
    lastN: 'Pence',
    fullName: 'Tom Pence'
  }, {
    firstN: 'Meagher',
    lastN: 'Pence',
    fullName: 'Meagher Pence'
  }, {
    firstN: 'Surv',
    lastN: 'dyal',
    fullName: 'Surv dyal'
  }
]

const fn = {};
const ln = {};

const uniqueNames = names.filter(name => {
  // Check if it already exists.const wasVisited = Boolean(fn[name.firstN]) || Boolean(ln[name.lastN]);

  // Record the visit:
  fn[name.firstN] = ln[name.lastN] = true;

  // return the verdictreturn !wasVisited;
});

console.log(uniqueNames)

Solution 2:

You seem to want to build a result where the first name and last name are both unique. I'd build an object with keys for first names and last names separately, then test both.

Note that this logic is a little different to yours, where for some reason you've rejected 'Tom Pence' even though according to the above logic the name should be in the result.

E.g.

var names = [{
    firstN: 'Dave',
    lastN: 'Mike',
    fullName: 'Dave Mike'
  },
  {
    firstN: 'Dave',
    lastN: 'Pence',
    fullName: 'Dave Pence'
  },
  {
    firstN: 'Tom',
    lastN: 'Pence',
    fullName: 'Tom Pence'
  }, {
    firstN: 'Meagher',
    lastN: 'Pence',
    fullName: 'Meagher Pence'
  }, {
    firstN: 'Surv',
    lastN: 'dyal',
    fullName: 'Surv dyal'
  }
]

functiongetUnique(data) {
  var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
  return data.reduce(function(result, obj) {
    if (!(obj.firstNin foundNames.firstNs || obj.lastNin foundNames.lastNs)) {
      foundNames.firstNs[obj.firstN] = true;
      foundNames.lastNs[obj.lastN] = true;
      result.push(obj);
    }
    return result;
  }, []);
}

console.log(getUnique(names))

If you want to filter out any duplicate for any value that has been seen so far, then the following does that:

var names = [{
    firstN: 'Dave',
    lastN: 'Mike',
    fullName: 'Dave Mike'
  },
  {
    firstN: 'Dave',
    lastN: 'Pence',
    fullName: 'Dave Pence'
  },
  {
    firstN: 'Tom',
    lastN: 'Pence',
    fullName: 'Tom Pence'
  }, {
    firstN: 'Meagher',
    lastN: 'Pence',
    fullName: 'Meagher Pence'
  }, {
    firstN: 'Surv',
    lastN: 'dyal',
    fullName: 'Surv dyal'
  }
]

functiongetUnique(data) {
  var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
  return data.reduce(function(result, obj) {
    if (!(obj.firstNin foundNames.firstNs || obj.lastNin foundNames.lastNs)) {
      result.push(obj);
    }
    foundNames.firstNs[obj.firstN] = true;
    foundNames.lastNs[obj.lastN] = true;
    return result;
  }, []);
}

console.log(getUnique(names))

Note that in both cases, the result will be unstable as it relies on the order that names are supplied to the source data array, e.g. given names in the order:

Tom Jones, Tom Smith, Mary Smith

only Tom Jones will be returned, but if the order is:

Tom Jones, Mary Smith, Tom Smith

then both Tom Jones and Mary Smith will be returned.

Post a Comment for "Searching For Unique Properties In The Array Object"