1

i have tow array for tow drop down list,and i need to exclude the selected value from the list each time. but it does not work

arr1 = [{label: "Syria",value: 1},{label: "USA",value: 2}]
arr2 = [{country:1}]

excludeSelectedCountry() {
  if ( arr1.length > 0) {
    return arr1.filter(
      country =>
        !arr2.some(e => {
          e.country && e.country === country.value;
        })
    );
  } else return arr2;
}

1 Answer 1

1

You can use .find:

let arr1=[{label: "Syria",value: 1},{label: "USA",value: 2}];
let arr2=[{country:1}];

const excludeSelectedCountry = () => {
     if (arr1.length > 0) {
          return arr1.filter(country => !arr2.find(e => e.country==country.value));
     } else {
          return arr2;
     }
}

console.log( excludeSelectedCountry() );

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

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.