Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
Find synced users from two arrays based on id and return array A with one column from array B

i am trying to find a list of synced users between two arrays (objArray1 & objArray2) and return the data from objArray1 along with 'aid' in Objarray2. I have the below code working, but i am trying to see if can return 'aid' from objArray2 as well in the below format.

Code sample below

// 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;
    });

Required JSON Format, please note that all matching items should be returned from objArray1, with the exception of 'aid' from objArray2

{
            "records": [
                {
                    "id": {aid},                // from objArray2
                    "fields": {
                        "Name":{name},          // from objArray1  
                        "sid": {id}             // from objArray1
                }
                ]
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Presented below is one possible way to achieve the desired objective.

Code Snippet

// 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 }

Explanation

Inline comments added to the snippet above.


EDIT Use name and id from array-1. Used restOfArr1Props to account for all other props of array-1 objects to be included.

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 }

about 4 years ago · Juan Pablo Isaza Denunciar

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));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda