Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.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 - Fatal编程技术网

Javascript 在两个数组上循环时查找公共值

Javascript 在两个数组上循环时查找公共值,javascript,arrays,Javascript,Arrays,在这种情况下,我需要比较并找到两个数组上的公共值。我很清楚如何使用一个,但不确定在这种情况下如何使用 我的第一个数组如下所示: [ { kind: 'E', path: [ 'short_name' ], lhs: 'testing', rhs: 'testing1' }, { kind: 'E', path: [ 'agent_name' ], lhs: 'testing', rhs: 'testing2' } ] [ { lhs: 'le

在这种情况下,我需要比较并找到两个数组上的公共值。我很清楚如何使用一个,但不确定在这种情况下如何使用

我的第一个数组如下所示:

[ { kind: 'E',
    path: [ 'short_name' ],
    lhs: 'testing',
    rhs: 'testing1' },
  { kind: 'E',
    path: [ 'agent_name' ],
    lhs: 'testing',
    rhs: 'testing2' } ]
[ { lhs: 'legacyId', rhs: 'id_number' },
  { lhs: 'name.short', rhs: 'short_name' },
  { lhs: 'name.long', rhs: 'agent_name' },
  { lhs: 'gender', rhs: 'gender' },
  { lhs: 'dob', rhs: 'date_of_birth' } ]
上面的数组表示与文档更改内容相关的信息

我的第二个数组如下所示:

[ { kind: 'E',
    path: [ 'short_name' ],
    lhs: 'testing',
    rhs: 'testing1' },
  { kind: 'E',
    path: [ 'agent_name' ],
    lhs: 'testing',
    rhs: 'testing2' } ]
[ { lhs: 'legacyId', rhs: 'id_number' },
  { lhs: 'name.short', rhs: 'short_name' },
  { lhs: 'name.long', rhs: 'agent_name' },
  { lhs: 'gender', rhs: 'gender' },
  { lhs: 'dob', rhs: 'date_of_birth' } ]
我需要做的是遍历并找到第一个数组元素中“path”的公共值,以及第二个数组的“rhs”值

因此,根据我在这里的示例,我应该找到以下值:
short\u name
agent\u name


如何编写一个循环在两个数组上执行此操作?

您可以在
for
中使用一个简单的
for

这是实现这一点最简单(可能不是最有效)的方法,从第一个数组中获取值,然后循环第二个数组。如果找到一个值,则将其推送到另一个数组(
results
)并中断第二个循环(如果一个值已经匹配,则无需继续运行)

var a=[{kind:'E',
路径:['short_name'],
lhs:‘测试’,
rhs:'测试1'},
{种类:'E',
路径:['agent_name'],
lhs:‘测试’,
rhs:'测试2'}]
var b=[{lhs:'legacyId',rhs:'id_number'},
{lhs:'name.short',rhs:'short_name'},
{lhs:'name.long',rhs:'agent_name'},
{lhs:'性别',rhs:'性别'},
{lhs:'出生日期',rhs:'出生日期'}]
var结果=[];
对于(变量i=0;iconsole.log(results)
您可以
减少第一个数组,并在第二个数组上使用
forEach
循环查看每个值是否相等,然后将值推送到累加器:

const arr1=[{kind:'E',path:['short_name'],lhs:'testing',rhs:'testing1'},{kind:'E',path:['agent_name'],lhs:'testing',rhs:'testing2'}]
const arr2=[{lhs:'legacyId',rhs:'id_number'},{lhs:'name.short',rhs:'short_name'},{lhs:'name.long',rhs:'agent_name'},{lhs:'gender',rhs:'gender'},{lhs:'dob',rhs:'date of_birth'}]
const common=arr1.reduce((a,o1)=>{
const match=arr2.find(o2=>o1.path[0]==o2.rhs)
匹配和a.push(match.rhs)
归还
}, [])
console.log(common)
通过嵌套一个循环并在第二个循环中搜索第一个循环的每个值,您可以在O(n^2)时间内完成此操作,也可以使用hashmap在O(n)时间内完成此操作。在JavaScript中,我们使用对象来实现这一点,因为它们的值可以在O(1)时间内访问

例如:

const array1 = [
  { kind: "E", path: ["short_name"], lhs: "testing", rhs: "testing1" },
  { kind: "E", path: ["agent_name"], lhs: "testing", rhs: "testing2" }
];

const array2 = [
  { lhs: "legacyId", rhs: "id_number" },
  { lhs: "name.short", rhs: "short_name" },
  { lhs: "name.long", rhs: "agent_name" },
  { lhs: "gender", rhs: "gender" },
  { lhs: "dob", rhs: "date_of_birth" }
];

const hashMap = {};
const commonValues = [];

for (let i = 0; i < array1.length; i++) {
  const currentValue = array1[i].path[0];
  hashMap[currentValue] = true;
}

for (let i = 0; i < array2.length; i++) {
  const currentValue = array2[i].rhs;
  if (hashMap[currentValue]) commonValues.push(currentValue);
}

//now commonValues contains all of the common between them. You may want to filter out duplicate matches, depends on your use case. 
常量数组1=[
{kind:“E”,路径:[“short_name”],lhs:“testing”,rhs:“testing1”},
{kind:“E”,路径:[“agent_name”],lhs:“testing”,rhs:“testing2”}
];
常数数组2=[
{lhs:“legacyId”,rhs:“id_number”},
{lhs:“name.short”,rhs:“short_name”},
{lhs:“name.long”,rhs:“agent_name”},
{lhs:“性别”,rhs:“性别”},
{lhs:“出生日期”,rhs:“出生日期”}
];
常量hashMap={};
常量commonValues=[];
for(设i=0;i

虽然看起来代码要多得多,但这样做可以节省大量的时间复杂性,因为您的数据会变得任意大。假设您的数据有100个项目长,一个O(n^2)解决方案需要10000次传递才能找到所有匹配项。与上述解决方案相反,它需要200次通过。这些节省的时间随着数据变大而迅速累积。

从第二个数组中的
rhs
值中设置
,并通过检查设置来减少第一个数组和过滤器路径

const arr1=[{kind:'E',
路径:['short_name'],
lhs:‘测试’,
rhs:'测试1'},
{种类:'E',
路径:['agent_name'],
lhs:‘测试’,
rhs:'测试2'}],
arr2=[{lhs:'legacyId',rhs:'id_number'},
{lhs:'name.short',rhs:'short_name'},
{lhs:'name.long',rhs:'agent_name'},
{lhs:'性别',rhs:'性别'},
{lhs:'出生日期',rhs:'出生日期'},
arr2Set=新集合(arr2.map(({rhs})=>rhs)),
matches=arr1.reduce((a,{path})=>[…a,…path.filter(p=>arr2Set.has(p))],[]);

console.log(matches)
我真的很喜欢这个解决方案的简洁性。@Ademo绝对是最可读的解决方案,但请记住,使用嵌套循环(reduce和.forEach都是循环)时,时间复杂度是O(n^2),因此如果您的数据很大,这个解决方案将运行得非常慢。不确定我为什么会被否决?请解释为什么我可以修复我的代码:)@CalIrvine它能工作吗?接受了吗?它解决了这个问题。OP没有要求性能优先的解决方案,否则我会相应地调整我的答案。我可以选择其他答案,因为它不是写在一行中的,但这不是被问到的问题:P@CalIrvine它的工作范围很广,也许没有你想要的那么快,但是现在计算机的速度非常非常快:)很遗憾你没有意识到这并不重要。当然,我应该努力使它尽可能高效,这是我在使用reduce的情况下做的,但我制作了一个可读且简短的版本。如果这些数据增长,数据库和查询将是这里的解决方案,而不是脚本。我想让你看到这一点。我尝试用更大的数据集测试我的两种方法,结果发现你的解决方案实际上慢了99%:/如果你能使你的解决方案快99%,我将删除我的否决票:)@Kobe the d