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

时间:2018-05-23 09:40:31

标签: javascript arrays underscore.js

此代码比较第一个数组名称以查看是否存在重复项,一旦找到重复项,将这些重复项与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,我不仅要知道名称,还要存储在part1中的所有关于George的信息,然后是存储在part2中的所有关于George的信息的另一个对象。

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

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

1 个答案:

答案 0 :(得分: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)

  })
}