Bishal Jain
Bishal Jain

Reputation: 209

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.

Upvotes: 0

Views: 735

Answers (2)

Aadmaa
Aadmaa

Reputation: 932

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;

}

Upvotes: 0

cmgchess
cmgchess

Reputation: 10247

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

Upvotes: 1

Related Questions