根据另一个数组将数据提取到对象数组中

Fetch data into an array of objects according to another array

此代码比较第一个数组名称以查看是否存在重复项,一旦找到重复项,这些重复项将与由 pl 和 pl2 创建的另外 2 个数组进行比较,如果在原始重复项和 pl 之间发现重复项或pl2,这些新的副本显示在控制台中。

var names = ["Nancy", "Patrick", "George", "Hecker", "George", "Nancy", 
"Robert", "Robert"]

var part1 = [
{height : 1.2, weight : 50, age: 55, name : "Nancy" },
{height : 2, weight : 60, age: 47, name : "Patrick" },
{height : 2, weight : 42, age: 24, name : "George" },
{height : 1.7, weight : 56, age: 58, name : "Hecker" },
{height : 1.1, weight : 39, age: 23, name : "Karin" }
]

var part2 = [
{height : 0.2, weight : 23, age: 45, name : "George" },
{height : 1.8, weight : 41, age: 29, name : "Moos" },
{height : 1.5, weight : 35, age: 25, name : "Ricard" }
]

setTimeout(function() {
var uniq = names
.map((name) => {
return {count: 1, name: name}
})
.reduce((a, b) => {
a[b.name] = (a[b.name] || 0) + b.count
return a
}, {})

var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)

console.log(duplicates) //output array with Nancy and George

checkInArr(duplicates);

}, 500);

function checkInArr(duplicates) {

var pl = _.pluck(part1, "name");
var pl2 = _.pluck(part2, "name");
var dup = _.intersection(pl, duplicates);
var dup2 = _.intersection(pl2, duplicates);

dup.forEach(function(element) {
console.log(element)

//Here is where I'm stuck, element regroup the name that are the same in 
both duplicates and part1
//Now I would like to fetch the other corresponding data in the part1 and 
part2 arrays of objects according to the names returned by element
//in this exemple : element returns Nancy and George so i would like to get 
Nancy and George infos of part1 (height, weight, age)

})

dup2.forEach(function(element) {
console.log(element)

//in this exemple : element returns only George so i would like to get 
George infos of part2 (height, weight, age)

})
}

但是,因为它写在代码里面,我想根据控制台中的名称和两个数组获取完整信息。

所以想要的结果将是相应名称的对象,而不仅仅是名称。例如:如果元素 returns George,我不仅想要名字,还想要存储在 part1 中的关于 George 的所有信息,然后是存储在 part2 中的关于 George 的所有信息的另一个对象。

我尝试了一些方法但没有成功,也没有在网上找到信息。 感谢您的帮助。

Fiddle : http://jsfiddle.net/nc2ac8ed/

您可以更改 checkInArr 函数以使用 Setfilter 来检查交叉点。

您的 duplicates 是您感兴趣的名称数组。通过从这些值中创建 Set,我们可以快速检查 "person" 是否相交:

const doesIntersect = nameSet.has(person.name);

这可以在 filter 中使用以清除非重复项。

var names = ["Nancy", "Patrick", "George", "Hecker", "George", "Nancy", "Robert", "Robert"]

var part1=[{height:1.2,weight:50,age:55,name:"Nancy"},{height:2,weight:60,age:47,name:"Patrick"},{height:2,weight:42,age:24,name:"George"},{height:1.7,weight:56,age:58,name:"Hecker"},{height:1.1,weight:39,age:23,name:"Karin"}];
var part2=[{height:.2,weight:23,age:45,name:"George"},{height:1.8,weight:41,age:29,name:"Moos"},{height:1.5,weight:35,age:25,name:"Ricard"}];

var uniq = names
  .map((name) => {
    return {count: 1, name: name}
  })
  .reduce((a, b) => {
    a[b.name] = (a[b.name] || 0) + b.count
    return a
  }, {})

var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)

console.log(duplicates) //output array with Nancy and George *and Robert*

checkInArr(duplicates);


function checkInArr(duplicates) {
  const dupSet = new Set(duplicates);
  const dup = part1.filter(p => dupSet.has(p.name));
  const dup2 = part2.filter(p => dupSet.has(p.name));

  dup.forEach(function(element) {
    console.log("dup", element)

  })

  dup2.forEach(function(element) {
    console.log("dup2:", element)

  })
}