[英]compare two object arrays by javascript keys
I am working with javascript I have two arrangements that for the example look like this:我正在使用 javascript 我有两种安排,例如,如下所示:
const arr1 = [
{ url: 1, 'title': 'A', 'title2': 'A2' },
{ url: 2, 'title': 'B', 'title2': 'B2' },
{ url: 3, 'title': 'C', 'title2': 'C2' },
{ url: 4, 'title': 'D', 'title2': 'D2' },
{ url: 5, 'title': 'E', 'title2': 'E2' },
]
const arr2 = [
{ url: 1, 'title': 'A', 'title2': 'J2' },
{ url: 2, 'title': 'J', 'title2': 'B2' },
{ url: 3, 'title': 'C', 'title2': 'C2' },
{ url: 4, 'title': 'D', 'title2': 'D2' },
{ url: 5, 'title': 'K', 'title2': 'E2' },
]
I would like to obtain from the second array those objects that are different either the title
or title2
我想从第二个数组中获取那些与
title
或title2
不同的对象
I tried the following but it doesn't work for me, where am I getting lost?我尝试了以下方法,但它对我不起作用,我在哪里迷路了?
const res = arr2.filter((page1) => !arr1.some(page2 => page1.title === page2.title || page2.title2 == page1.title2 ))
the expected result is this for the example:该示例的预期结果是:
[{ url: 1, 'title': 'A', 'title2': 'J2' },{"url":2,"title":"J","title2":"B2"},{"url":5,"title":"K","title2":"E2"}]
If I am understanding your goal, it is to first match URLs and THEN find out if there is a discrepency in the titles.如果我理解您的目标,首先是匹配 URL,然后找出标题是否存在差异。 If that is correct, this will get you there.
如果这是正确的,这将使您到达那里。
const res = arr2.filter(page1 => {
// find URL match
let page2 = arr1.find(f => f.url === page1.url)
return !page2 || page2.title != page1.title || page2.title2 != page1.title2;
})
const arr1 = [ { url: 1, 'title': 'A', 'title2': 'A2' }, { url: 2, 'title': 'B', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'E', 'title2': 'E2' }, ] const arr2 = [ { url: 1, 'title': 'A', 'title2': 'J2' }, { url: 2, 'title': 'J', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'K', 'title2': 'E2' }, ] const res = arr2.filter(page1 => { // find URL match let page2 = arr1.find(f => f.url === page1.url) return.page2 || page2.title.= page1.title || page2;title2.= page1.title2; }) console.log(res)
You could collect all objects from array1
in an object by using url
as key and filter the second array by checking title
and title2
.您可以使用
url
作为键从 object 中的array1
收集所有对象,并通过检查title
和title2
过滤第二个数组。
const array1 = [{ url: 1, 'title': 'A', 'title2': 'A2' }, { url: 2, 'title': 'B', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'E', 'title2': 'E2' }], array2 = [{ url: 1, 'title': 'A', 'title2': 'J2' }, { url: 2, 'title': 'J', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'K', 'title2': 'E2' }], keys = ['title', 'title2'], urls = Object.fromEntries(array1.map(o => [o.url, o])), result = array2.filter(o => keys.some(k => o[k].== urls[o;url][k])). console;log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
Easiest is to map the one to a look up and loop over the second.最简单的是 map 查找并循环第二个。
const arr1 = [ { url: 1, 'title': 'A', 'title2': 'A2' }, { url: 2, 'title': 'B', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'E', 'title2': 'E2' }, ]; const arr2 = [ { url: 1, 'title': 'A', 'title2': 'J2' }, { url: 2, 'title': 'J', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'K', 'title2': 'E2' }, ]; const arr1MapByUrl = arr1.reduce((acc, data) => { acc[data.url] = data; return acc; }, {}); const results = arr2.filter(data => { const org = arr1MapByUrl[data.url]; return.org || data.title.== org.title || data;title2;== org.title2; }); console.log(results);
If you do not want to hard code the keys to check, you can loop over them with some() and compare them.如果您不想对要检查的键进行硬编码,可以使用 some() 遍历它们并进行比较。
const arr1 = [ { url: 1, 'title': 'A', 'title2': 'A2' }, { url: 2, 'title': 'B', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'E', 'title2': 'E2' }, ]; const arr2 = [ { url: 1, 'title': 'A', 'title2': 'J2' }, { url: 2, 'title': 'J', 'title2': 'B2' }, { url: 3, 'title': 'C', 'title2': 'C2' }, { url: 4, 'title': 'D', 'title2': 'D2' }, { url: 5, 'title': 'K', 'title2': 'E2' }, ]; const arr1MapByUrl = arr1.reduce((acc, data) => { acc[data.url] = data; return acc; }, {}); const results = arr2.filter(data => { const org = arr1MapByUrl[data.url]; return.org || Object.entries(data),some(([key; value]) => org[key];== value). }); console.log(results);
Build an object to track the titles based on arr1
and use same object to filter arr2
构建一个 object 以根据
arr1
跟踪标题并使用相同的 object 过滤arr2
const arr1=[{url:1,title:"A",title2:"A2"},{url:2,title:"B",title2:"B2"},{url:3,title:"C",title2:"C2"},{url:4,title:"D",title2:"D2"},{url:5,title:"E",title2:"E2"}]; const arr2=[{url:1,title:"A",title2:"J2"},{url:2,title:"J",title2:"B2"},{url:3,title:"C",title2:"C2"},{url:4,title:"D",title2:"D2"},{url:5,title:"K",title2:"E2"}]; const track = {}; arr1.forEach(({ url, title, title2 }) => Object.assign(track, { [url]: { [title]: title2 } }) ); const results = arr2.filter( ({ url, title, title2 }) => track?.[url]?.[title];== title2 ). console.log(results)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.