1

I have a set of values (groups) in a comma delimited string and would like to check if any of those values match any array items (reqRights) and return true or false, but is returns undefined error.

const reqRights = ["18900253","3217840","1053"]; 
const groups    = "3217635,18272308,1053,3217633,18900253,3217698,3217699,3217840,10162510";

function checker(value) {
  var groups = groups.split(",");
  console.log(groups);
  return groups.every(function(v) {
    return value.indexOf(v) !== -1;
  });
}

arr = reqRights.filter(checker);
console.log(arr); 

js engine SpiderMonkey 1.8, does not support .includes and some methods

2
  • 2
    just a side not: Doing groups.split(","); every single index just makes your loop so much slower. Do it outside so you are not constantly splitting the string up. Commented Sep 8, 2022 at 14:01
  • var groups = groups.split You can not reuse the same variable. That is your error. Rename one. Commented Sep 8, 2022 at 14:02

5 Answers 5

1

You're using const on groups, so you cannot reassign it. And you also should move groups.split(",") outside of checker function to avoid creating a new array every time calling checker.

some can help you to check regRights item is in groups instead of every.

const reqRights = ["18900253","3217840","1053"]; 
const groups = "3217635,18272308,1053,3217633,18900253,3217698,3217699,3217840,10162510";

const groupsArray = groups.split(",");

function checkGroupValue(group) { 
   return group === value;
}

function checker(value) {
  return groupsArray.some(checkGroupValue);
}

arr = reqRights.filter(checker);
console.log(arr); 

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

2 Comments

Thanks Nick, i think the js engine running on my webapp doesnt support some method ( line 281: syntax error (line='const even = (element) => element % 2 === 0; ' token='> element % 2 === 0; '). )
@DavidGarcia hmm your code in your comment is not in the question, I'm not sure where that error is from. By the way, I just modify my answer to use function explicitly. Hopefully, it can help you out.
1
const reqRights = ["18900253","3217840","1053"]; 
const groupsConstant    = "3217635,18272308,1053,3217633,18900253,3217698,3217699,3217840,10162510";

function checker(value) {
  var groups = groupsConstant.split(",");
  return groups.every(v => groups.indexOf(v) !== -1);
}

arr = reqRights.filter(checker);
console.log('elements matched: ', arr.length ? 'yes' : 'no'); 

Comments

1

I was trying to avoid the indexOf as it might give a false positive result (example: reqRight "1053" would be listed if found in "1053568") THis is not necessarily the shortest way, but should be compatible with spiderMonkey 1.8

const reqRights = ["18900253","3217840","1053"]; 
const groups    = "3217635,18272308,1053,3217633,18900253,3217698,3217699,3217840,10162510";

function checker(values, required) {
  var valuesArray = values.split(",");
  return valuesArray.filter((value) => {
    return required.filter((req) => {
      return req === value; 
    }).length > 0;
  })
}

arr = checker(groups, reqRights);
console.log(arr);

Comments

1

Use Array.find in filtering from groups:

const reqRights = ["18900253","3217840","1053"]; 
const groups    = "3217635,18272308,1053,3217633,18900253,3217698,3217699,3217840,10162510";
const checker = value => 
 ({[value]: reqRights.find(v => value === v) ? true : false});
const checked = groups.split(`,`).map(checker);
console.log(checked);

Or maybe concat both (groups as array) arrays, sort that numerically and filter the result on double values.

const reqRights = ["18900253","3217840","1053"]; 
const groups    = "3217635,18272308,1053,3217633,18900253,3217698,3217699,3217840,10162510";
const checked = groups.split(`,`)
  .concat(reqRights)
  .sort( (a, b) => +a - +b)
  .filter((v, i, self) => self[i+1] === v);
  
console.log(checked);

Comments

0

You can simply achieve this by using Array.map() along with Array.some() method.

Live Demo :

const reqRights = ["18900253","3217840","1053"]; 
const groups    = "3217635,18272308,1053,3217633,18900253,3217698,3217699,3217840,10162510";

const res = groups.split(',').map(item => {
    return reqRights.some(n => item === n)
});

console.log(res);

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.