Home > OS >  Find similar items from an array filtered by the provided data
Find similar items from an array filtered by the provided data

Time:10-06

trying to solve the problem here. so I have an array of objects and the object contains some properties and their values. so I want to find the objects that have similar values filtered by the props that I'm providing.

here's the code that I've done but the issue is I need to have all the object's properties have the same values as each other.

const data = [
    {
        id: 1,
        name: 'ali',
        class: 12,
        thisSec: true
    },
    {
        id: 2,
        name: 'vicks',
        class: 11,
        thisSec: false
    },
    {
        id: 3,
        name: 'ali',
        class: 12,
        thisSec: true
    },
    {
        id: 4,
        name: 'vicks',
        class: 11,
        thisSec: false
    },
    {
        id: 5,
        name: 'nick',
        class: 10,
        thisSec: true
    },
    {
        id: 6,
        name: 'lick',
        class: 12,
        thisSec: false
    },
]

const checkFilter = (id, searchCritera) => {
    if(id && searchCritera.length) {
        const selectedItem = data.find(item => item.id === id)
        
        const dataneeded = []
        searchCritera.forEach((item) => {
            data.forEach(dataItem => {
                if(dataItem.id !== selectedItem.id) {
                    if(dataItem[item] === selectedItem[item]) {
                        const ifElemExists = dataneeded.find(dataNeddedItem => dataNeddedItem.id == dataItem.id)
                        if(!ifElemExists) {
                            dataneeded.push(dataItem)   
                        }
                    }
                }
            })
        })
        
        console.log('dataneeded ', dataneeded);
    }
}

checkFilter(1, [
    'name',
    'class'
])

output: enter image description here

I am filtering with name and class props but the output I'm getting is having the name as different values.

any help would be really appreciated. thanks.

CodePudding user response:

Try this filter

const checkFilter = (id, searchCritera) => {
    if(id && searchCritera.length) {
        var keyToIgnore = ["id"];
        const selectedItem = data.find(item => item.id === id)
        const targetWithoutSelectedItem = data.filter(item => item !== selectedItem);
        const targets = targetWithoutSelectedItem.filter(item => {
          return Object.keys(item).every(obj => keyToIgnore.includes(obj) || item[obj] === selectedItem[obj] );
        })

        console.log('dataneeded ', targets);
    }
}

CodePudding user response:

You can use the Array#filter method to filter based on selectedItem properties supplied. To exclude selectedItem use o.id !== id as in the demo below:

const data = [ { id: 1, name: 'ali', class: 12, thisSec: true }, { id: 2, name: 'vicks', class: 11, thisSec: false }, { id: 3, name: 'ali', class: 12, thisSec: true }, { id: 4, name: 'vicks', class: 11, thisSec: false }, { id: 5, name: 'nick', class: 10, thisSec: true }, { id: 6, name: 'lick', class: 12, thisSec: false }, ],

checkFilter = (id, searchCritera) => {
    if(id && searchCritera.length) {
        const selectedItem = data.find(item => item.id === id);
        const dataneeded = data.filter(
            o => o.id !== id && o.name === selectedItem.name && o.class === selectedItem.class
        );       
        console.log('dataneeded ', dataneeded);
    }
};

checkFilter(1, [
    'name',
    'class'
]);

  • Related