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

问题描述 投票:-1回答:1

此代码比较第一个数组名称以查看是否存在重复项,一旦找到重复项,将这些重复项与pl和pl2创建的另外两个数组进行比较,如果在原始重复项和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)

})
}

但是,由于它是在代码中编写的,我希望根据控制台中的名称和两个数组获得完整的信息。

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

我尝试过一些东西,但没有成功,也没有在网上找到信息。谢谢你的帮助。

小提琴:http://jsfiddle.net/nc2ac8ed/

javascript arrays underscore.js
1个回答
0
投票

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

你的duplicates是你感兴趣的一系列名字。通过用这些值创建一个Set,我们可以快速检查“人”是否相交:

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)

  })
}
© www.soinside.com 2019 - 2024. All rights reserved.