¡Tengo estos dos conjuntos de objetos y quiero obtener valores únicos de ellos! ¿Alguien sabe cómo puedo hacer eso en JS?
let array1 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, ]; let array5 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, { categoryName: "category 5" }, ]; let newArray = []; for (const arr5Item of array5) { for (const arr1Item of array1) { if (arr5Item.categoryName !== arr1Item.categoryName) { newArray.push(arr5Item); break; } else { newArray.push(arr1Item); break; } } } console.log("newArray ", newArray);Array.reduce puede resolver su problema fácilmente.
Paso 1: fusione toda su matriz en una matriz, por three dots en javascript.
Paso 2: Usa Array.reduce para devolver tu resultado.
let array1 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, ]; let array5 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, { categoryName: "category 5" }, ]; const mergedArray = [...array1, ...array5]; const uniqueArray = mergedArray.reduce((acc, item) => { if (!acc.some(e => e.categoryName == item.categoryName)) { acc.push(item); } return acc; }, []); console.log(uniqueArray);Puede lograr fácilmente el resultado usando Set . Usar Set es una forma eficiente
1)
let array1 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, ]; let array5 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, { categoryName: "category 5" }, ]; const set = new Set([ ...array1.map((o) => o.categoryName), ...array5.map((o) => o.categoryName), ]); const result = [...set.keys()].map((s) => ({ categoryName: s })); console.log(result);2)
let array1 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, ]; let array5 = [ { categoryName: "category 1" }, { categoryName: "category 2" }, { categoryName: "category 3" }, { categoryName: "category 4" }, { categoryName: "category 5" }, ]; const set = new Set([ ...array1.map((o) => o.categoryName), ...array5.map((o) => o.categoryName), ]); const result = []; for (let categoryName of set) result.push({ categoryName }); console.log(result);No sólo uno. Hay muchos métodos disponibles para esto. Básicamente necesitas una lógica de bucle.
Encuentre la implementación de Array.reduce .
Lógica
violín de trabajo
let array1 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" }]; let array5 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" },{ categoryName: "category 5" }]; const uniqueValues = [...array1, ...array5].reduce((acc, curr) => { const nodeExist = acc.some(item => item.categoryName === curr.categoryName); const secondArrayExist = array5.some(item => item.categoryName === curr.categoryName); if (!nodeExist) { acc.push(curr); } return acc; }, []); console.log(uniqueValues);Si desea obtener los nodos que están presentes solo en cualquiera de la matriz, use la lógica a continuación.
Lógica
array1 y array5 . Si el nodo no está presente en ninguno de array1 o array5 , entonces este es un nodo único.violín de trabajo
let array1 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" }]; let array5 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" },{ categoryName: "category 5" }]; const uniqueValues = [...array1, ...array5].reduce((acc, curr) => { const firstArrayExist = array1.some(item => item.categoryName === curr.categoryName); const secondArrayExist = array5.some(item => item.categoryName === curr.categoryName); if (!firstArrayExist || !secondArrayExist) { acc.push(curr); } return acc; }, []); console.log(uniqueValues);