在数组对象中搜索唯一属性

时间:2021-07-31 20:14:39
     var name = [ {
 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
 }
  ................and so on like 100s
 ]

So I want to find unique names so that names occur only once

所以我想找到唯一的名称,以便名称只出现一次

So my answer from above data sample should be Dave Mike and Surv Dyal

所以我从上面的数据样本中回答的应该是Dave Mike和Surv Dyal

For this I have gotten only this far

为此,我得到了这么远

var list =Object.keys(newList).map( function(key){

    var temp_firstName = newList[key].firstName + ','+ key;
    var temp_lastName = newList[key].lastName + ','+ key;
    console.log( temp_lastName,temp_firstName );

    return temp_lastName, temp_firstName;
});

console.log(list);
//console.log(newList);

}

2 个解决方案

#1


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.

array.map是一个错误的使用方法,因为它是一对一的数组转换。您正在寻找的是一种可以修剪数组的方法,可以是array.filter或array.reduce,也可以是一个记录您已经遇到的名称的临时键值存储。

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 verdict
  return !wasVisited;
});

console.log(uniqueNames)

#2


1  

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.

请注意,这个逻辑与您的逻辑略有不同,出于某种原因,您拒绝了“Tom Pence”,即使根据上述逻辑,名称应该在结果中。

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'
  }
]

function getUnique(data) {
  var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
  return data.reduce(function(result, obj) {
    if (!(obj.firstN in foundNames.firstNs || obj.lastN in 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'
  }
]

function getUnique(data) {
  var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
  return data.reduce(function(result, obj) {
    if (!(obj.firstN in foundNames.firstNs || obj.lastN in 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.

那么汤姆琼斯和玛丽史密斯都将被退回。

#1


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.

array.map是一个错误的使用方法,因为它是一对一的数组转换。您正在寻找的是一种可以修剪数组的方法,可以是array.filter或array.reduce,也可以是一个记录您已经遇到的名称的临时键值存储。

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 verdict
  return !wasVisited;
});

console.log(uniqueNames)

#2


1  

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.

请注意,这个逻辑与您的逻辑略有不同,出于某种原因,您拒绝了“Tom Pence”,即使根据上述逻辑,名称应该在结果中。

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'
  }
]

function getUnique(data) {
  var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
  return data.reduce(function(result, obj) {
    if (!(obj.firstN in foundNames.firstNs || obj.lastN in 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'
  }
]

function getUnique(data) {
  var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
  return data.reduce(function(result, obj) {
    if (!(obj.firstN in foundNames.firstNs || obj.lastN in 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.

那么汤姆琼斯和玛丽史密斯都将被退回。