0

If I want to get a value from the object know but the case is different, it won’t match: "Hello" won’t match "hello". Is there another work around for this?

function ct(e) {
  var know = {
    "Hi": "Hello",
    "How are you?": "Great",
    "Bye": "Have A Nice Day !",
    "Hello": "Hi , Whats Up",
    "What's your name?": "Claire Rachael"
  };
  let t = document.createElement("span");
  
  t.setAttribute("class", "chatMessage left");
  
  if (e in know) {
    setTimeout(() => {
      t.textContent = "Typing...";
      d.appendChild(t);
      g = d.getAttribute("data-bottom")
        ? d.getAttribute("data-bottom")
        : 0;
      
      if (parseInt(g) < 200)
        d.scrollTo(0, d.scrollHeight);
    }, 2000);
    setTimeout(() => {
      t.textContent = know[e];
      d.appendChild(t);
      g = d.getAttribute("data-bottom")
        ? d.getAttribute("data-bottom")
        : 0;
      
      if (parseInt(g) < 200)
        d.scrollTo(0, d.scrollHeight);
      else
        alert("new message");
    }, 6000)
  }
  else {
    t.textContent = "I don't understand";
    d.appendChild(t);
  }
}
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Victor
  • 29
  • 1
  • 6
  • `Object.keys(know).map(k => k.toLowerCase()).includes(e.toLowerCase())` - of course then `t.textContent = know[e];` would have to change – Bravo Mar 21 '22 at 12:01
  • And [`find`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find) to get the property value. Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212) and use the static and instance methods of [`Object`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Mar 21 '22 at 12:02

1 Answers1

1

You can't do that with in, no.

You can create a map of the properties keyed by their name in lower case, then look in that map using only lower-case values:

const knowMap = new Map(Object.entries(know).map(([key, value]) => [key.toLowerCase(), value]));
const elower = e.toLowerCase();
// ...
if (knowMap.has(elower)) {
    // ...you can use `knowMap.get(elower)` here to get the value...
    // ...e.g.: `t.textContent = knowMap.get(elower);`
}

Live Example:

var know = {
    "Hi": "Hello",
    "How are you?": "Great",
    "Bye": "Have A Nice Day !",
    "Hello": "Hi , Whats Up",
    "What's your name?": "Claire Rachael"
};

const knowMap = new Map(Object.entries(know).map(([key, value]) => [key.toLowerCase(), value]));
// ...
function test(key) {
    const keylower = key.toLowerCase();
    if (knowMap.has(keylower)) {
        const value = knowMap.get(keylower);
        console.log(`value for "${key}" is "${value}"`);
    } else {
        console.log(`No match for key "${key}"`);
    }
}
test("Hi");
test("HI");
test("hi");

If you know that none of your properties has the value undefined, you can avoid going to the map twice:

const value = knowMap.get(elower);
if (value !== undefined) {
    // ...use `value` here...
}

Live Example:

var know = {
    "Hi": "Hello",
    "How are you?": "Great",
    "Bye": "Have A Nice Day !",
    "Hello": "Hi , Whats Up",
    "What's your name?": "Claire Rachael"
};

const knowMap = new Map(Object.entries(know).map(([key, value]) => [key.toLowerCase(), value]));
// ...
function test(key) {
    const value = knowMap.get(key.toLowerCase());
    if (value !== undefined) {
        console.log(`value for "${key}" is "${value}"`);
    } else {
        console.log(`No match for key "${key}"`);
    }
}
test("Hi");
test("HI");
test("hi");
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    @Oleksii - **Thanks** for pointing that out! Stupid typo, it was missing `(know).map` in the middle of that. (Kind of surprised no one else -- for instance, the OP -- noticed.) Fixed it, thanks again! – T.J. Crowder Mar 23 '22 at 07:41