0
\$\begingroup\$

I have four arrays of the same object which I use for a form.
They work as pairs such as list_1 contains all possible values and list_1_filtered contains the choosen values, and the same goes with list_2 and list_2_filtered. The thing is that they can share similar objects with the same object.id and I want to filter it to not add values in list_2_filtered if it has the same object.id as any values in list_1_filtered.
So I did this :

getList2(form_field_value:string){
    return this.list_2.filter(
        (e: /*Object*/)=>
            e.name.indexOf(form_field_value) != -1 &&
            this.list_2_filtered.indexOf(e)==-1 &&
            this.checkList2NotInList1(e)
    )
}

checkList2NotInList1(object: /*Object*/): boolean {
    for (let i = 0; i < this.list_1_filtered.length; i++) {
      if (object.id== this.list_1_filtered.at(i).id) {
        return false;
      }
    }
    return true;
  }

But I find it quite unpleasant to use another function to filter based on field in my object. Is there a better way to do it ?

/!\ Names are changed for better understanding - please do not judge naming /!\

UPDATE :

class Object :

{
    id: string,
    itemName : string,
    quantity : number
}

Data in List 1 :

id itemName quantity
J254 corn 154
I465 wheat 169
O25845 strawberry 200
J365 corn 205

Data in List 1 filtered :

id itemName quantity
J254 corn 154

Those works for a tag field in the form such as list_1_filtered contains the selected tags and list_1 contains all possible tags. Same goes for list_2 & list_2_filtered

Data in List 2 :

id itemName quantity
J254 corn 154
P48612 Pasta 3010
E754 egg 312
J365 corn 205

Data in List 2 filtered :

id itemName quantity
P48612 Pasta 3010

getList2 is used when the user writes in the input field to return suggestions. But a tag can't be in list_2_filtered if its already in list_1_filtered. So getList2 shouldn't return {id:J254,itemName:corn,quantity:154} if the user writes "J2" in the input field because {id:J254,itemName:corn,quantity:154} is already in list_1_filtered

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to the Code Review community. Please don't change the names when posting the question. The code must be working code from a project you have written. Please read How do I ask a good question? and A guide to Code Review for Stack Overflow users. \$\endgroup\$
    – pacmaninbw
    Jan 13 at 14:08
  • 1
    \$\begingroup\$ Hey, name were also changef for privacy reasons --> client's name + the code is working well \$\endgroup\$
    – Neo
    Jan 13 at 14:31
  • \$\begingroup\$ @Neo are you able to give example datasets? So I can make sure I'm not misunderstanding your question \$\endgroup\$ Jan 15 at 3:57
  • \$\begingroup\$ @JaniceZhong Updated the question with data and a bit mor explanation, hope that this will help you \$\endgroup\$
    – Neo
    Jan 16 at 9:12

1 Answer 1

0
\$\begingroup\$

Can't you use Set?

const set = new Set([...list1, ...list2]);
const results = Array.from(set.values()).filter(item => item.name === object.name)

Might not be 100% suitable to your case, but you get the idea

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, that's a pretty good idea didn't think about it. It would simply things, but yeah, using a set would be a difficult for the rest of my code, so not really suitable. \$\endgroup\$
    – Neo
    Jan 13 at 10:50
  • 2
    \$\begingroup\$ This answer is borderline for code review. Please read Please read How do I write a good answer and A guide to Code Review for Stack Overflow users. \$\endgroup\$
    – pacmaninbw
    Jan 13 at 14:04
  • \$\begingroup\$ FYI, your other 3 answers are good. \$\endgroup\$
    – pacmaninbw
    Jan 14 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.