Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据另一个数组将数据提取到对象数组中_Javascript_Arrays_Underscore.js - Fatal编程技术网

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

Javascript 根据另一个数组将数据提取到对象数组中,javascript,arrays,underscore.js,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 :

此代码比较第一个数组名称以查看是否存在重复项,一旦找到重复项,这些重复项将与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)

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

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,我不仅想要名称,还想要第1部分中存储的所有关于George的信息,然后是第2部分中存储的所有关于George的信息的另一个对象

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


Fiddle

您可以更改
checkInArr
功能,以使用
Set
filter
检查交叉点

您的
副本
是您感兴趣的名称数组。通过使用这些值创建
集合
,我们可以快速检查“人”是否相交:

const doesIntersect = nameSet.has(person.name);
这可用于
过滤器
中,以清除非重复项

var name=[“南希”、“帕特里克”、“乔治”、“赫克”、“乔治”、“南希”、“罗伯特”、“罗伯特”]
VarPart1=[{身高:1.2,体重:50,年龄:55,姓名:“南希”},{身高:2,体重:60,年龄:47,姓名:“帕特里克”},{身高:2,体重:42,年龄:24,姓名:“乔治”},{身高:1.7,体重:56,年龄:58,姓名:“赫克”},{身高:1.1,体重:39,年龄:23,姓名:“卡琳”};
VarPart2=[{身高:2,体重:23,年龄:45,姓名:“乔治”},{身高:1.8,体重:41,年龄:29,姓名:“穆斯”},{身高:1.5,体重:35,年龄:25,姓名:“里卡德”}];
var uniq=名称
.map((名称)=>{
返回{count:1,name:name}
})
.减少((a,b)=>{
a[b.name]=(a[b.name]| | 0)+b.count
归还
}, {})
var duplicates=Object.keys(uniq).filter((a)=>uniq[a]>1)
console.log(重复)//与Nancy、George*和Robert一起输出数组*
核对者(副本);
功能检查器(重复){
const dupSet=新集合(重复);
constdup=part1.filter(p=>dupSet.has(p.name));
const dup2=part2.filter(p=>dupSet.has(p.name));
重复forEach(函数(元素){
console.log(“dup”,元素)
})
dup2.forEach(函数(元素){
日志(“dup2:,元素)
})

}
请同时添加想要的结果。