Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda