Estoy tratando de encontrar una lista de usuarios sincronizados entre dos matrices (objArray1 y objArray2) y devolver los datos de objArray1 junto con 'ayuda' en Objarray2. Tengo el siguiente código funcionando, pero estoy tratando de ver si puedo devolver 'ayuda' de objArray2 también en el siguiente formato.
Ejemplo de código a continuación
// SystemA const objArray1 = [ { id: "1", name: "John" }, { id: "2", name: "Jack" }, { id: "3", name: "Sam" }, { id: "4", name: "Bill" }, ]; // SystemB const objArray2 = [ { id: "1", name: "John", aid:"uuud2905555" }, { id: "3", name: "Sam" }, aid:"uuud29029303" { id: "5", name: "Bob" }, aid:"uuud29435454" ]; const array1IDs = objArray1.map((item) => { return item.id }) // Result: array1IDs = ["1", "2", "3", "4"]; const array2IDs = objArray2.map((item) => { return item.id }) // Result: array2IDs = ["1", "3", "5"]; // FIND SYNCED USERS // Compare the id value of each item in objArray1 with each item of objArray2 // Return the ones that match. const syncedUsers = objArray1.filter((item) => { const found = objArray2.find((element) => element.id === item.id); return found; });Formato JSON requerido, tenga en cuenta que todos los elementos coincidentes deben devolverse de objArray1, con la excepción de 'ayuda' de objArray2
{ "records": [ { "id": {aid}, // from objArray2 "fields": { "Name":{name}, // from objArray1 "sid": {id} // from objArray1 } ] }A continuación se presenta una posible forma de lograr el objetivo deseado.
Fragmento de código
// method to create result as "JSON" const createJSON = (arr1, arr2) => ( // use "stringify" to transform JavaScript object into JSON JSON.stringify({ // set-up the "records" prop records: arr2 .filter( // filter to keep only those that have matching "id" ({ id }) => arr1.some(ob => ob.id === id) ) .map( // de-structure & rename props to match desired objective ({ id : sid, name : Name, aid: id }) => ({ id, fields: {Name, sid} }) ) }) ); // SystemA const objArray1 = [ { id: "1", name: "John" }, { id: "2", name: "Jack" }, { id: "3", name: "Sam" }, { id: "4", name: "Bill" }, ]; // SystemB const objArray2 = [ { id: "1", name: "John", aid:"uuud2905555" }, { id: "3", name: "Sam", aid:"uuud29029303" }, { id: "5", name: "Bob", aid:"uuud29435454" }, ]; console.log('result as a JSON: ', createJSON(objArray1, objArray2)); .as-console-wrapper { max-height: 100% !important; top: 0 }Explicación
Se agregaron comentarios en línea al fragmento anterior.
EDITAR Use el name y la id de la matriz-1. Usó restOfArr1Props para tener en cuenta todos los demás accesorios de los objetos array-1 que se incluirán.
const createJSON = (arr1, arr2) => ( JSON.stringify({ records: arr1 .filter( ({ id }) => arr2.some(ob => ob.id === id) ) .map( ({ id : sid, name : Name, ...restOfArr1Props }) => ({ id: arr2.find(a2 => a2.id === sid)?.aid ?? 'missing aid', fields: { Name, sid, ...restOfArr1Props }, }) ) }) ); // SystemA const objArray1 = [ { id: "1", name: "John", prop1: 'value11', prop2: 'value12' }, { id: "2", name: "Jack", prop1: 'value21', prop2: 'value22' }, { id: "3", name: "Sam", prop1: 'value31', prop2: 'value32' }, { id: "4", name: "Bill", prop1: 'value41', prop2: 'value42' }, ]; // SystemB const objArray2 = [ { id: "1", name: "John", aid:"uuud2905555" }, { id: "3", name: "Sam", aid:"uuud29029303" }, { id: "5", name: "Bob", aid:"uuud29435454" }, ]; console.log('result as a JSON: ', createJSON(objArray1, objArray2)); .as-console-wrapper { max-height: 100% !important; top: 0 }const objArray1 = [ { id: '1', name: 'John', type: 'bully' }, { id: '2', name: 'Jack', type: 'sporty' }, { id: '3', name: 'Sam', type: 'kind' }, { id: '4', name: 'Bill', type: 'poliet' }, ]; const objArray2 = [ { id: '1', name: 'John', aid: 'uuud2905555' }, { id: '3', name: 'Sam', aid: 'uuud29029303' }, { id: '5', name: 'Bob', aid: 'uuud29435454' }, ]; const syncedUsers = { records: [] }; for (let user1 of objArray1) { const foundUser = objArray2.find(user2 => user2.id === user1.id); if (foundUser) { syncedUsers.records.push({ id: foundUser.aid, fields: { sid: user1.id, name: user1.name, ...user1 }, }); } } console.log(JSON.stringify(syncedUsers));