angularjs custom filter to check for values inside a data array

后端 未结 5 956
野趣味
野趣味 2020-12-02 02:34

I have two filters which filter the data according to the queue key in the data. Here is my code :

5条回答
  •  被撕碎了的回忆
    2020-12-02 03:14

    If you want to filter $scope.servers with the values of the queue you can try this. Hope this helps.

    const servers = [
        {name:'ServerA', queuearr:[{'queue' :'111'}]},
        {name:'Server7', queuearr:[{'queue' :'111'}]},
        {name:'Server2', queuearr:[{'queue' :'456'}]},
        {name:'ServerB', queuearr:[{'queue' :'456'}]},
    ];
    
    const itemsToCheck = { 'PAV':'111', 'UAT':'426' };
    
    const filter = (arr, itemsToCheck) => arr.filter((item) => {
    
        for (let v of Object.values(itemsToCheck)) {
    
            const found = item.queuearr.find(({ queue }) => queue === v);
    
            if (found) return true;
        }
    
        return false;
    });
    
    console.log(filter(servers, itemsToCheck));

提交回复
热议问题