Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!