3

im facing the issue when trying to implement some conditional rendering. Here is the context:

the data:

groups_a : [
             {id:1} ,
             {id:2}
            ]

groups_b : [
             {id:1} ,
             {id:3}
            ]

The condition:

I wish to be check each item in groups_b , whether they exist in groups_a . So in this case , the condition should return back true because groups_b has id:1 and so does groups_a

JSX :

            {###the condition ##
                         ?
            <Button>Approve</Button>   
            :
            null
            } 
3
  • Both groups seems to be object (with incorrect syntax), it should be enclosed in [] instead of {} Commented Jun 1, 2020 at 9:18
  • groups_a and groups_b are not valid JavaScript objects. Are you sure those variables are not type of Array? Which would make more sense. Commented Jun 1, 2020 at 9:19
  • Hello i apologize , yes it should be an array , have updated my question Commented Jun 1, 2020 at 9:21

3 Answers 3

4

You could do this assuming groupA/groupB to be array of object

const groupA = [{id:1}, {id:3}, {id:4}];
const groupB = [{id:1}, {id:3}];

const boolean = groupB.every(obj => groupA.find(aObj => obj.id === aObj.id));
console.log('Method 1:', boolean)
// this will be true if every object in B is included in A

// If these are extremely large arrays you can improve performance by converting groupA into obj, and then checking by that object

const groupAHashMap = groupA.reduce((acc, cur) => ({...acc, [cur.id]: true}), {});
const boolean2 = groupB.every(obj => groupAHashMap[obj.id]);
console.log('Method 2:', boolean2)

Sign up to request clarification or add additional context in comments.

4 Comments

Would it be possible to use the find to return true if at least one is present?
@neowenshun if u want the answer one only one is present then just replace every with some, groupB.some(obj => groupA.find(aObj => obj.id === aObj.id)
the second solution returns false ! @ehab
@HamidShoja yea thx for spotting the error, i forgot to add the initial accumulator for the reduce function
0

checkout this

function App(){ 

    const groupA = [{id:1}, {id:2}];
    const groupB = [{id:1}, {id:3}];

    const gourpAIds = groupA.map(item => item.id);

    return groupB.map(item => {
        if(gourpAIds.includes(item.id)){
            return <Button>Approve</Button>   ;
        }else{
            return null;
        }
    });

}

Comments

0

it's an other solution that you can get the common list also.

const groupA = [{id:1}, {id:3}, {id:4}];
const groupB = [{id:1}, {id:2}];



const ListCommon = groupA.filter(x => groupB.find(y => x.id === y.id));

console.log('result:', ListCommon.length>0)
console.log('List:', ListCommon)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.