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!

3
  • 1
    Use for-loop to iterate array not for-in..Later should be used to iterate keys of Object Commented Jun 21, 2016 at 17:37
  • if dateArray is actually an array then use forEach or for(var i= 0; i < dateArray.length; i++){ Commented Jun 21, 2016 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 Commented Jun 21, 2016 at 17:39

3 Answers 3

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.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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>).

5 Comments

Any advantage of using for-in loop to iterate array
No advantage, it's generally a bad idea. Since he's using jQuery, you could use $.each().
@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!
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);

       }
    }
});

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.