-3

My array

var obj = [{"2021-07-30":"8:00 AM - 10:00 AM"},{"2021-07-30":"12:00 PM - 2:00 PM"}];

How can I return true if the key and value pair match? Here there is a duplicate "date" because of that I am not able to get the code correctly?

My code

var date = "2021-07-30";
var time = "12:00 PM - 2:00 PM";

if ( date in obj[0] && obj[0][date] == time) {
    console.log('exists');
    console.log(obj[0][date]);
} else {
    console.log('does not exist');
}

Above code returns "does not exist", even though that key and value pair exist.

Swasthik
  • 61
  • 2
  • 10
  • Duplicate key on json, if you change to: var obj = [{"2021-07-30":"8:00 AM - 10:00 AM"},{"2021-07-30":"12:00 PM - 2:00 PM"}]; it works (see: https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object) – Rafael Souza Jul 30 '21 at 16:42
  • 1
    A JSON cannot have duplicate keys. – Nitheesh Jul 30 '21 at 16:47
  • @RafaelSouza : Thank you for looking into this. I changed the format and works for only the first set of key and value – Swasthik Jul 31 '21 at 03:00
  • @Swasthik Your given time does not match the time you check against. `"8:00 AM - 10:00 AM"` != `12:00 PM - 2:00 PM`, at index 0, so of course your code outputs 'does not exist'. – Daedalus Jul 31 '21 at 03:06
  • There's always [Object.entries(yourthing).find(...)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) – Mike 'Pomax' Kamermans Jul 31 '21 at 03:07
  • @Daedalus: Thank you. Yeah correct but how can I make this dynamic? – Swasthik Jul 31 '21 at 03:38
  • @Mike'Pomax'Kamermans: I already looked into this but I need "else" condition as well. I want to do some operations if not key-value pair does not exist as well. – Swasthik Jul 31 '21 at 03:39
  • Why do you think this should work by only examining `obj[0]`? You don't even look at `obj[1]`. I realize you edited your answer from one that had a nonsensical object with duplicate keys, but your proposed code makes less sense now, given what you now know about not being able to have duplicate keys. I think you need to try again armed with what you've learned, or ask your question in a coherent way with code that at least matches the intent of your approach. – Wyck Jul 31 '21 at 03:48

1 Answers1

2

You can iterate over the array with a forEach loop, and then use a for...in loop to iterate over each object's keys (assuming they can have more than one key/value pair, though your example shows only 1 key/value pair per object). Then just check each key and value. This should handle duplicates nicely.

const datetimes = [{"2021-07-30":"8:00 AM - 10:00 AM"},{"2021-07-30":"12:00 PM - 2:00 PM"}];

const targetDate = "2021-07-30";
const targetTime = "12:00 PM - 2:00 PM";

datetimes.forEach(datetime => {
  for (const date in datetime) {
    if (date === targetDate && datetime[date] === targetTime) {
      console.log('exists!')
      console.log(datetime[date])
    }
  }
})

Here's a working fiddle

Dharman
  • 30,962
  • 25
  • 85
  • 135
Matt
  • 251
  • 1
  • 5
  • Could you please look into my fiddle : https://jsfiddle.net/SwasthikUK/d49vro7m/ I am trying to build a calendar with available and unavailable timeslots Here you can see for Jul 31st > "8:00 AM - 10:00 AM" shows twice, And for Aug 1st > "12:00 PM - 2:00 PM" shows twice actually, it should show only once. Background color "gray" is an unavailable slot Background color "blue" is available slot – Swasthik Jul 31 '21 at 06:15
  • I have applied your code and it works fine but there is a small bug where it shows unavailable timeslots twice. – Swasthik Jul 31 '21 at 06:38
  • Your fiddle is the entire calendar component. Please link a minimal reproduceable example of the problem you're seeing. https://stackoverflow.com/help/minimal-reproducible-example I tried looking through the fiddle, but the code is confusing imo. – Matt Jul 31 '21 at 07:24
  • I create another question here by explaining it in detail. Please check https://stackoverflow.com/questions/68607688/how-to-show-unavailable-time-slot-in-my-calendar – Swasthik Aug 01 '21 at 11:43