0

I have an object and I want to test the name of the property, not the content but the name. I can't achieve it:

theCtns = {
  "services": {
    "a": {
      "image": "a"
    },
    "b": {
      "image": "b"
    }
  }

alreadyExistSrv = false;
regex = new RegExp('(^|\\s)' + "b" + '(?=\\s|$)', 'g');
for (var service in theCtns.services) {
  if (regex.test(theCtns.services[service])){
    alreadyExistSrv = true;
  }
}

if(alreadyExistSrv){
  console.log("the name is free");
}else{
  console.log("the name already exists");
}

alreadyExistSrv = false;
regex = new RegExp('(^|\\s)' + "bb" + '(?=\\s|$)', 'g');
for (var service in theCtns.services) {
  if (regex.test(theCtns.services[service])){
    alreadyExistSrv = true;
  }
}

if(alreadyExistSrv){
  console.log("the name is free");
}else{
  console.log("the name already exists");
}

so when I use "b" in regex it should say the name already exists and when I use "bb" it should say the name is free

Jerome
  • 1,162
  • 2
  • 16
  • 32

0 Answers0