Home > Enterprise >  Comparing a particular key and value in JavaScript
Comparing a particular key and value in JavaScript

Time:05-21

I have an object named session_name, and I need to see if the date and trainer_id have equal values or not; how will I do this?

let session_name =
                [               
                {
                    trainer_id: '',
                    date: '',
                    start_time: '',
                    end_time:'',
                },
                    
                {
                    trainer_id: '',
                    date: '',
                    start_time: '',
                    end_time:'',
                };
                    
        ]

updated details are below.

var values = [
 {
                trainer_id: '01',
                date: '25/01/22',
                start_time: '10:55',
                end_time:'15:55',
            },
            {
                trainer_id: '02',
                date: '25/02/22',
                start_time: '10:55',
                end_time:'15:55',
            },
            {
                trainer_id: '01',
                date: '25/01/23',
                start_time: '13:55',
                end_time:'15:55',
            },
             {
                trainer_id: '01',
                date: '22/05/22',
                start_time: '13:55',
                end_time:'15:55',
            }
             ];

         // appraoch one
         // checking the id and date to be true(true if its duplicate)
       var valueArr = values.map(function(item){ return item.trainer_id, 
       item.date });
       var isDuplicate = valueArr.some(function(item, idx){ 
           // return valueArr.indexOf(item) != idx 
        return valueArr.indexOf(item, idx   1) !== -1
       });
          console.log('Is same trainer is assigned for the same date?? 
         :-'  isDuplicate);
         if (isDuplicate === true){
       // checking thw end time is the start time of the trainer..
         }

ok so the problem is how do I check if the same trainers are falling in the same allotted time if the conditions are true i.e if (duplicate === true){

CodePudding user response:

Guessing your array will always have two objects inside:

function compare(arr){
 return arr[0].trainer_id === arr[1].trainer_id && arr[0].date === arr[1].date;
}

And if you want a more dynamic approach (selecting which entries to compare, comparing all arrays inside object) you can do this:

function compare(arr,...rest) {
  return rest.every(item => {
    return arr.every(obj => obj[item] === arr[0][item]);
  });
}

You use the above function like this:

compare(session_name, "trainer_id", "start_time");

OR

compare(session_name, "date", "trainer_id", "end_time");

Whatever combination of entry names you wish

CodePudding user response:

You can make a function where you pass the index and the keys so that it compares. That way it will only compare the items and keys you want.

const session_name = [{
    trainer_id: '1',
    date: '',
    start_time: '',
    end_time: '',
  },
  {
    trainer_id: '2',
    date: '',
    start_time: '',
    end_time: '',
  }
]

function compareKeys(arr, index1, index2, key1, key2) {
  return arr[index1][key1] === arr[index2][key1] && arr[index1][key2] === arr[index2][key2];
}

console.log('are equal', compareKeys(session_name, 0, 1, 'trainer_id','date') )

CodePudding user response:

You can do something like this

let session_name = [{
    trainer_id: '123',
    date: '123',
    start_time: '',
    end_time: '',
  },

  {
    trainer_id: '',
    date: 'not equal',
    start_time: '',
    end_time: '',
  },
];
const compare = (obj) => {
  return obj.trainer_id === obj.date;
}
session_name.map(obj => {
  const isEqual = compare(obj);
  if (isEqual) {
    console.log('equal');
  } else {
    console.log('not equal');
  }
})
// If Want check For every Obj in that array Together
const isTrueForAll = session_name.map(obj => {
  return compare(obj);
}).every(v => v);
Update: After Unders

let session_name = [{
    trainer_id: '123',
    date: '123',
    start_time: '',
    end_time: '',
  },

  {
    trainer_id: '',
    date: '122',
    start_time: '',
    end_time: '',
  },
];
const compare = (obj, trainer_id, date) => {
  return obj.trainer_id === trainer_id && date === obj.date;
}
session_name.map(obj => {
  const isEqual = compare(obj, session_name.trainer_id, session_name.date);

  if (isEqual) {
    console.log('equal');
  } else {
    console.log('not equal');
  }
})

tanding what you really want after seeing other answers

  • Related