Estoy trabajando con javascript. Tengo dos arreglos que para el ejemplo se ven así:
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' }, ] Me gustaría obtener de la segunda matriz aquellos objetos que son diferentes, ya sea el title o el title2
Intenté lo siguiente pero no me funciona, ¿dónde me estoy perdiendo?
const res = arr2.filter((page1) => !arr1.some(page2 => page1.title === page2.title || page2.title2 == page1.title2 ))el resultado esperado es este para el ejemplo:
[{ url: 1, 'title': 'A', 'title2': 'J2' },{"url":2,"title":"J","title2":"B2"},{"url":5,"title":"K","title2":"E2"}]Si entiendo su objetivo, es primero hacer coincidir las URL y LUEGO averiguar si hay una discrepancia en los títulos. Si eso es correcto, esto te llevará allí.
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)Puede recopilar todos los objetos de array1 en un objeto utilizando url como clave y filtrar la segunda matriz comprobando title y 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; }Lo más fácil es asignar uno a una búsqueda y recorrer el segundo.
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);Si no desea codificar las claves para verificar, puede recorrerlas con some() y compararlas.
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);