1

I have a JSON array and I want to compare its values with a String. For Example, My JSON Array is

{
  "slot": ["10:00 AM", "10:30 AM", "03:30 PM"]
}

Now I am trying to access it in this way:

for (i in data.slot) {
  console.log(slot[i] === "10:00 AM"); //this return false
  if (slot[i] === "10:00 AM") btn1.disabled = true;
  if (slot[i] === "10:30 AM") btn2.disabled = true;
  if (slot[i] === "03:00 PM") btn3.disabled = true;
  if (slot[i] === "03:30 PM") btn4.disabled = true;
}

Now whenever I am trying to compare it, the if statement returns false and the button doesn't get disabled. I got to know this on the Chrome Dev console. Any Suggestions would be appreciated.

7
  • That should work. You should use else if since the comparisons are mututally exclusive, but it shouldn't make a difference. Commented Nov 11, 2020 at 18:51
  • Please post a minimal reproducible example. You can use a Stack Snippet to make it executable. Commented Nov 11, 2020 at 18:51
  • It should log true for i == 0, and false for all other iterations. Commented Nov 11, 2020 at 18:52
  • Just write let data = {...} Commented Nov 11, 2020 at 18:53
  • And idk why it is not working like I looked into Chrome Dev Console for the values I am getting and that is also right Commented Nov 11, 2020 at 18:53

2 Answers 2

4

You need to address the right variable

data.slot[i]
^^^^^

const data = { slot: ["10:00 AM", "10:30 AM", "03:30 PM"] }

for (let i in data.slot) {
  console.log(data.slot[i] === "10:00 AM"); // true false false
  if (data.slot[i] === "10:00 AM")
    btn1.disabled = true;
  if (data.slot[i] === "10:30 AM")
    btn2.disabled = true;
  if (data.slot[i] === "03:00 PM")
    btn3.disabled = true;
  if (data.slot[i] === "03:30 PM")
    btn4.disabled = true;
}
<button id="btn1">1</button>
<button id="btn2">2</button>
<button id="btn3">3</button>
<button id="btn4">4</button>

A better approach takes the value directly by iterating with for ... of statement

const data = { slot: ["10:00 AM", "10:30 AM", "03:30 PM"] }

for (const value of data.slot) {
    console.log(value === "10:00 AM"); // true false false
    if (value === "10:00 AM") btn1.disabled = true;
    if (value === "10:30 AM") btn2.disabled = true;
    if (value === "03:00 PM") btn3.disabled = true;
    if (value === "03:30 PM") btn4.disabled = true;
}
<button id="btn1">1</button>
<button id="btn2">2</button>
<button id="btn3">3</button>
<button id="btn4">4</button>

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

3 Comments

Hi @Nina Scholz You are absolutely correct. I know this could work like this but my json array is coming from a servlet and Idk why it is not working
Hi @Nina Scholz your 2nd solution runs PERFECTLY. THANKS, A TONNN !!!!!
maybe you have no data or need to parse the JSON string.
0
const data = {
  slot: ["10:00 AM", "10:30 AM", "03:30 PM"],
};

for (i in data.slot) {
  const _slot = data.slot[i];
  console.log(_slot === "10:00 AM");
  if (_slot === "10:00 AM") btn1.disabled = true;
  if (_slot === "10:30 AM") btn2.disabled = true;
  if (_slot === "03:00 PM") btn3.disabled = true;
  if (_slot === "03:30 PM") btn4.disabled = true;
}

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.