Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search though a long list of javascript objects to find the first instance of 'sent: 0'

Tags:

javascript

Having a major looping problem here.

My data looks like this:

var mailouts =
   { signUp: { date: '', sent: 1 },
     lesson1: { sent: 1, time: 20 },
     lesson2: { sent: 0, time: 20 },
     lesson3: { sent: 0, time: 20 },
     lesson4: { sent: 0, time: 20 },
     lesson5: { sent: 0, time: 20 } 
     }

I need to go through the mailouts object to find which lesson is next to be sent (aka which is the first instance of sent: 0 when going through the lessons in order). I don't have control over the data structure so I need to work with this model.

The only way I can think of is really ugly and unmanageable (there are actually about 50 'lesson' objects, not just 5, just kept it short for brevity.).

The only thing I can think of that will vaguely work is something like this:

if(mailouts.lesson1.sent === 1){
    if(mailouts.lesson2.sent === 1){
        if(mailouts.lesson3.sent === 1){
            if(mailouts.lesson4.sent === 1){
                if(mailouts.lesson5.sent === 1){
                    // Everything has been sent
                }else{
                    // Send lesson5
                }
            }else{
                // Send lesson4
            }
        }else{
            // Send lesson3
        }
    }else{
        // Send lesson2
    }
}else{
    // Send lesson1
}

Surely Javascript must have an easier way to deal with this, but I'm drawing blanks. Any ideas?

EDIT: just to clarify, I only need to send one 'lesson' every time this script runs - only whichever one comes next in the list.

like image 297
JVG Avatar asked Feb 03 '26 02:02

JVG


2 Answers

This code handles dealing the the lessons in order -- even if they aren't in order in the object. Iterating object properties does NOT guarantee any kind of order, so you must enforce than itself.

var mailouts =
   { signUp: { date: '', sent: 1 },
     lesson1: { sent: 1, time: 20 },
     lesson2: { sent: 0, time: 20 },
     lesson3: { sent: 0, time: 20 },
     lesson4: { sent: 0, time: 20 },
     lesson5: { sent: 0, time: 20 } 
     }

var firstLessonWithNoSent;

for (var lessonNum = 0; lessonNum < 50; lessonNum++) {
  var lesson = mailouts["lesson" + lessonNum];

  if (lesson && lesson.sent === 0) {
    firstLessonWithNoSent = lesson;
    break;
  }
}
like image 123
Jeremy J Starcher Avatar answered Feb 05 '26 15:02

Jeremy J Starcher


Just to make it clear: You're using plain JavaScript object so you cannot be sure about the order of elements when you're iterating it. It would be much better if they were an array.

If we're dealing with an object, you could use for .. in loop like this:

for (var lesson in mailouts) {
    if (mailouts.hasOwnProperty(lesson)) {
        if (lesson.sent === 0) {
            // there it is!
        }
    }
}

But again, you're dealing with sequential data so using an object doesn't make any sense. You should be using an array instead. So it could look like this:

var mailouts = [
    { date: '', sent: 1 },
    { sent: 1, time: 20 },
    { sent: 0, time: 20 },
    { sent: 0, time: 20 },
    { sent: 0, time: 20 },
    { sent: 0, time: 20 }
];

If we have an array then the solution depends on the environment You can use ES6 Array.prototype.find if that's implemented:

mailouts.find(function (el) {
    return el.sent === 0;
});

if not, a simple forEach will suffice:

mailouts.forEach(function (el) {
    if (el.sent === 0) {
        // there it is!
    }
});
like image 42
Michał Miszczyszyn Avatar answered Feb 05 '26 14:02

Michał Miszczyszyn