1

I am trying to find if second array has any string from the first one. Not sure what I am doing wrong

const domainAlertList = ["@domain1", "@tomato2", "@carrot3"];

const search = ["[email protected]", "[email protected]"];

const myFunc = () => {
  return search.some((r) => domainAlertList.includes(r));
};

console.log(myFunc());

It returns false instead of true

6
  • this could help. It is basically the same question you asked. Commented Mar 5, 2022 at 10:36
  • You need to change the test; [email protected] includes @domain1, not the other way around. Commented Mar 5, 2022 at 10:37
  • @ChrisG if you could write the code that would be helpful Commented Mar 5, 2022 at 10:39
  • @Zirix my code is exactly same but it doesn't work Commented Mar 5, 2022 at 10:39
  • Here's one way: jsfiddle.net/b9s53ha6 Commented Mar 5, 2022 at 10:43

5 Answers 5

2

const domainAlertList = ["@domain1", "@tomato2", "@carrot3"];

const search = ["[email protected]", "[email protected]"];

const myFunc = () => {
  return domainAlertList.some((r) => {
      return search.some(t => t.includes(r))
  });
};

console.log(myFunc());

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

Comments

1

There are many ways to approach this. One of them could be the indexOf() method.

let str = "[email protected]";

str.indexOf('domain1') !== -1 // true

source

Comments

0

You have to map through the second array too like this

const domainAlertList = ["@domain1", "@tomato2", "@carrot3"];

const search = ["[email protected]", "[email protected]"];

const myFunc = () => {
  return search.some((r) => domainAlertList.some(domain=>{domain.includes(r)});
};

console.log(myFunc());

Comments

0

You can iterate and use array filter function.

const domainAlertList = ["@domain1", "@tomato2", "@carrot3"];
const search = ["[email protected]", "[email protected]"];

let r = []
domainAlertList.forEach(i => {
  r.push( search.filter(i1 =>  i1.includes(i)) )  
})
newArr = r.map((a) => {
  if(a.length != 0) {
    return a
  }
}).filter((a) => a);
console.log(newArr)

Comments

0
const domainAlertList = ["@domain1", "@domain1", "@carrot3"];

const search = ["[email protected]", "[email protected]"];

const myFunc = () => {
  return domainAlertList.some((domain) =>
    search.some((email) => email.includes(domain))
  );
};

console.log(myFunc());

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.