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

275
Vistas
Remove duplicate from the nested Object of Array

I am trying to remove the duplicate from the below array of Object which has some nested element with array

Below is my array.

      {
        field: ‘A’,
        value: {
          key_A: ['ajd', 'ajd', 'kajd'],
        },
      },
      {
        field: ‘A’,
        value: {
          key_B: ['123', '4', '45', '94'],
        },
      },
      {
        field: 'A',
        value: {
          key_A: ['ajd', 'ajd', ''],
        },
      },
      {
        field: ‘A’,
        value: {
          key_B: ['123', '4', '45'],
        },
      },
      {
        field: ‘Z’,
        value: {
          key_A: ['ajdm', 'askjd', 'kajd'],
        },
      },
      {
        field: ‘Z’,
        value: {
          key_B: ['13', '123', '1823'],
        },
      },
      {
        field: ‘Z’,
        value: {
          key_A: ['ajdm', 'askjd', ''],
        },
      },
      {
        field: ’Z’,
        value: {
          key_B: ['13', '123', ''],
        },
      },
    ];

and I want to keep the 1st key under each field i.e 1st key_A under field 'A' and 1st key_B under field 'A'

same for field 'Z'

so final o/p:

      {
        field: ‘A’,
        value: {
          key_A: ['ajd', 'ajd', 'kajd'],
        },
      },
      {
        field: ‘A’,
        value: {
          key_B: ['123', '4', '45', '94'],
        },
      },
      {
        field: ‘Z’,
        value: {
          key_A: ['ajdm', 'askjd', 'kajd'],
        },
      },
      {
        field: ‘Z’,
        value: {
          key_B: ['13', '123', '1823'],
        },
      },
    ];

I tried some logic with filter looping over the array but for the nested could not figure out a logic.

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

A possible implementation using the answer provided here. the findIndex gives the index of first element that satisfies the condition and the filter therefore filters out the first elements that satisfies the condition.
Object.keys(t.value)[0] gives either "key_A" or "key_B" depending on the element

let arr =    [  {        field: 'A',        value: {          key_A: ['ajd', 'ajd', 'kajd'],        },      },      {        field: 'A',        value: {          key_B: ['123', '4', '45', '94'],        },      },      {        field: 'A',        value: {          key_A: ['ajd', 'ajd', ''],        },      },      {        field: 'A',        value: {          key_B: ['123', '4', '45'],        },      },      {        field: 'Z',        value: {          key_A: ['ajdm', 'askjd', 'kajd'],        },      },      {        field: 'Z',        value: {          key_B: ['13', '123', '1823'],        },      },      {        field: 'Z',        value: {          key_A: ['ajdm', 'askjd', ''],        },      },      {        field:'Z',        value: {          key_B: ['13', '123', ''],        },      },    ];

let res = arr.filter((v, i, self) =>
  i === self.findIndex((t) => (
    t.field === v.field && Object.keys(t.value)[0] === Object.keys(v.value)[0]
  ))
)

console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Gelvez Denunciar

0

Something like this should do it.

interface Address {
    address: string,
    city: string,
    state: string,
    zip: number
}

const addresses: Address[] = [
    {
        address: "1234 Main St",
        city: "San Diego",
        state: "CA",
        zip: 92014
    },
    {
        address: "4444 Main St",
        city: "San Diego",
        state: "CA",
        zip: 92014
    },
    {
        address: "5555 Main St",
        city: "San Diego",
        state: "CA",
        zip: 92014
    },
    {
        address: "6666 Main St",
        city: "San Diego",
        state: "CA",
        zip: 92014
    },
    {
        address: "7777 Main St",
        city: "San Diego",
        state: "CA",
        zip: 92014
    }
]

function delay(time: number) {
    return new Promise(resolve => setTimeout(resolve, time));
  }


const apiCaller = async(addr: Address) => { 
    await delay(0.5);
    return `Got ${addr.address}`
}

const apiRunner = async () => {
    const results: PromiseSettledResult<string>[] = [];

    let promises: Promise<string>[] = [];

    for (const address of addresses) {
        promises.push(apiCaller(address));
        if (promises.length >= 10) {
            const rez = await Promise.allSettled(promises);
            results.push(...rez);
            promises = [];
        }
    }

    if (promises.length > 0) {
        const rez = await Promise.allSettled(promises);
        results.push(...rez);
    }

    return results;

}
about 4 years ago · Santiago Gelvez 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