A solution to your problem..
The function myfilter()
returns a array with unique id
values, and if any exist with available: true
it takes precedence
var values = [
{ id: 10, available: true },
{ id: 10, available: false },
{ id: 11, available: true },
{ id: 12, available: false }
];
function myfilter()
{
var filtarray=[];
values.forEach(function(element) {
var idxfound = filtarray.findIndex(function(ele2) {
return ele2.id === element.id;
});
if (idxfound==-1)
{
filtarray.push(element);
}
else {
if (filtarray[idxfound].available==false && element.available==true)
filtarray[idxfound].available=true;
}
});
console.log(filtarray);
return filtarray;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…