0

So I have multiple objects that I get from an AJAX request that looks like [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object].

So I

for (var prop in dateArray) {
     if (dateArray[prop].date === sdate) {

     }
}

sdate is a variable that has a list of dates. That I get from jQuery UI and dateArray is all my objects.

My object looks like:

0: Object
   available : true
   date : "01/01/2017"
   stock_qty : 0
   time: "07:30pm"
   title: "The seeing"

So I can iterate over them, and do I check to say if var A matches dates in var B do something. I'm struggling in how I then append the information in the object that matches to the element on the DOM.

Within my loop, when I find a match, i'm trying to write all the info in the objects into the DOM via html attr but i'm struggling. But i'm not sure how I refer to the match, like within my loop when it matches a date with another date, how can I then pull out all the objects from that match?

It's quite a vague question I know, sorry! I know what i'm trying to do, I just can't figure out how i'd do it in JavaScript.

Thanks in advance!

pourmesomecode
  • 4,108
  • 10
  • 46
  • 87
  • 1
    Use `for-loop` to iterate array not `for-in`..Later should be used to iterate `keys` of `Object` – Rayon Jun 21 '16 at 17:37
  • if dateArray is actually an array then use forEach or `for(var i= 0; i < dateArray.length; i++){` – Zargold Jun 21 '16 at 17:38
  • 1
    Hi Smurf, so for example if you want all the titles on the same date you would like to append all of those object together and show them? If you could make a codepen that has a dataset in the JS section that would be helpful :D – Kyle Pastor Jun 21 '16 at 17:39

3 Answers3

1

Assuming that sdate is an array of dates...
You can loop to check if each of your object.date is in this array.

I used jQuery inArray()

for(i=0; i<dateArray.length; i++){
    if($.inArray(dateArray[i].date,sdate)){
        // If the date defined in an object is found in the «list» sdate...
        // Do something.
    }
}

Edit
An alternative, that would also work if sdate is an array or a comma delimited string, is:

Here, I used JavaScript indexOf() method.

for(i=0; i<dateArray.length; i++){
    if(sdate.indexOf(dateArray[i].date)!=-1){
        // If the "indexOf" a defined object.date is found (not ==-1) in the «list» sdate...
        // Do something.
    }
}
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
0

Let's imagine in your DOM you have an element for each of the objects that you would like to display.

Then it could be something like:

for (var prop in dateArray) {
     if (dateArray[prop].date === sdate) {
        $(".date-holder").append(dateArray[prop].date);
        $(".stock-holder").append(dateArray[prop].stock_qty);
        //...
        //and so on
     }
}

Where you would insert the value of each object into an empty element (e.g. <div class="date-holder"></div>).

Sina
  • 765
  • 1
  • 13
  • 32
  • Any advantage of using `for-in` loop to iterate `array` – Rayon Jun 21 '16 at 17:45
  • 2
    No advantage, it's generally a bad idea. Since he's using jQuery, you could use `$.each()`. – Barmar Jun 21 '16 at 17:46
  • @Barmar, very true. It merely depends on the structure of the code, I just tried to give the idea of how it works, and jQuery's fanciness can be done in every way! – Sina Jun 21 '16 at 17:49
  • 1
    http://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript/9329476#9329476 – Barmar Jun 21 '16 at 17:51
  • [___`Array iteration and for...in`___](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...in#Array_iteration_and_for...in) – Rayon Jun 21 '16 at 17:58
0

if sdate is a list of dates you can check that a specific date (d) is in that list with either lo-dash _.includes or running a for loop:

so this will have to be nested inside the other for loop:

dateArray.forEach(
function(dateObjFromServer){
   for (var i=0; i < sdate.length; i++){
       if (dateObjFromServer.date === sdate[i]){
          //do something... 
          //for example: to set an attribute:
          document.getElementById('date-holder').setAttribute('data-date', dateObjFromServer);

       }
    }
});
Zargold
  • 1,892
  • 18
  • 24