Home > database >  Being able to remove duplicate keys from an array of objects
Being able to remove duplicate keys from an array of objects

Time:12-26

I have a question about how I can delete the existing elements, for example, in my case "Tallas" is repeated, could you please help me? Thank you very much to those who are willing to help me to solve this problem

const data = 
  [ { atributos: { Tallas:  [{ id: 0, name: 'XS' }, { id: 1, name: 'S'   }] }} 
  , { atributos: { Calzado: [{ id: 0, name: '10' }, { id: 1, name: '9.5' }] }} 
  , { atributos: { Tallas:  [{ id: 0, name: 'XS' }, { id: 1, name: 'S'   }] }} 
  ] 

the idea is to have this json format to get specified attributes for each product that i am looping through.

const expected = 
  [ { atributos: { Tallas:  [{ id: 0, name: 'XS' }, { id: 1, name: 'S'   }] }} 
  , { atributos: { Calzado: [{ id: 0, name: '10' }, { id: 1, name: '9.5' }] }} 
  ] 

How do I do this is there a way to do it, I've tried with filter plus the findindex but I can't get to eliminate the repetition of the json res= new.filter((arr, index, self) => index === self.findIndex( (t) => (t.attributes === arr.attributes )))

CodePudding user response:

this is a function that takes an array and return the same array but delete every duplicated item

function removeDuplicates(arr) {
return arr.filter((item, 
index) => arr.indexOf(item) === index);
}

I didn't understant the part written in spanish so I hope this is what you are looking for

CodePudding user response:

To unique the array of objects, we can use the Javascript Set module, if the array has complex nested objects, we can stringify each object before creating new Set data. this below function will unique the array of complex objects.

function unique_array(array = []) {
     const newSetData = new Set(array.map((e) => JSON.stringify(e)));
     return Array.from(newSetData).map((e) => JSON.parse(e));
}

CodePudding user response:

This is a solution specific to your question. this is not a generic solution.

const data = [
    {
        atributos: {
            Tallas: [
                { id: 0, name: "XS" },
                { id: 1, name: "S" },
            ],
        },
    },
    {
        atributos: {
            Calzado: [
                { id: 0, name: "10" },
                { id: 1, name: "9.5" },
            ],
        },
    },
    {
        atributos: {
            Tallas: [
                { id: 0, name: "XS" },
                { id: 1, name: "S" },
            ],
        },
    },
];

function uniqueArray(array) {
    const resultObject = array.reduce((acc, eachValue) => {
        let keys = Object.keys(eachValue.atributos);
        keys.forEach((eachKey) => {
            if (!acc[eachKey]) {
                acc[eachKey] = [];
            }
            let list = eachValue["atributos"][eachKey].map(
                (each) => each.id   "-"   each.name
            );
            acc[eachKey].push(...list);
        });
        return acc;
    }, {});

    const resultArray = Object.keys(resultObject).reduce((acc, each) => {
        let setData = Array.from(new Set(resultObject[each]));
        acc.push({
            atributos: {
                [each]: setData.map((e) => {
                    return { id: e.split("-")[0], name: e.split("-")[1] };
                }),
            },
        });
        return acc;
    }, []);

    return resultArray;
}

const result = uniqueArray(data)
console.log("result ", JSON.stringify(result, null, 2));

  • Related