So I have a select menu that lets the user select multiple options. the selected options correspond to actorModel.
Then I have an object array and I wish to filter it according to the selected options.
If the user selected option A and B , then it will return two objects from actorDocs.
The one that has a label value 'A' and the one that has the label value 'B'.
If no option is selected (actorModel is null) then it will return all objects from actorDocs.
The problem I am encountering in this code is that it only works if the user selects one option. If the user selects more than one, then I tink it is trying to locate an object that has several labels instead of several objects with each label.
Any help is more than welcome
const actorModel = ref({ 0:'label1', 1:'label2', 3:'label3'})
const actorDocs = ref([{'label':'label1'},{'label':'label2'},{'label':'label3'},{'label':'label4'}])
const actorListTest2 = computed(() => {
if (actorModel.value == null){var ttt = actorDocs.value}
else {
var ttt = actorDocs.value.filter(obj => {
return (
(!actorModel.value.length || obj.label.includes(actorModel.value) )
) })}
lenActordata.value = ttt.length
return ttt
});
Try like following snippet:
const actorModel = {0:'label1', 1:'label3'}
const actorDocs = [{'label':'label1'}, {'label':'label2'}, {'label':'label3'}, {'label':'label4'}]
const actorListTest2 = () => {
if (actorModel == null){
return actorDocs
} else {
return actorDocs.filter(obj => [...Object.values(actorModel)].includes(obj.label))
}
};
console.log(actorListTest2())