0

Got issue My javascript array contains dates for ex. 19.4.2003,28.11.1997,3.1,,29.6,7.11.2008 (date-month-year). As you can see some of dates are missing ( just ,, ) and some are without a year (for ex. 29.6).
The array above should looks like: 19.4,28.11,3.1,,29.6,7.11 Then I need to find an element and get its index with jQuery.inArray("searchVar", myArr); The code is below

if (data.response) { //response from social network
    var f_cnt = data.response.length;
    for(var i=0;i<f_cnt;i++) { 
        holidaysArr.push(data.response[i].bdate); //pushing date in array
        //modify in loop? or call function to modify later?
    }
    holidaysLoaded=true; // just flag
}       

Pushing (ok) -> modifying -> searching for element. Need your help.

UPDATE
While making project understand I need to call social network API more than once so saved data in my array. Check it:

    if (data.response) {
    friends_data = data.response.sort(sFirstName); //sorting nvm
    f_cnt = data.response.length; //count of arrayfields
    holidaysLoaded=true;    //flag
}

So array has uid, first_name, last_name, bdate atm. Not just bdate as hour ago. Question is: How to modify all friends_data[i].bdate with match? Empty birthdays have undefined atm. I think I need something like

for(var i=0;i<f_cnt;i++){
   friends_data[i].bdate = cutFunc(friends_data[i].bdate);
}

and form cutFunc(param) with checking on undefined, if not it will match with your function?

UPDATE 2 cutFunc should be smth like this:

function cutFunc(param) {
if(param==undefined) return
else return param.match(/\d+\.\d+/g);
}

thank you :)

1
  • Hi, if you're using my solution you just have to change holidaysArr.push(data.response[i].bdate); to holidaysArr.push(cutFunc(data.response[i].bdate));. Also, I would return ""; from the cutFunc if param==undefined. Commented Jan 15, 2012 at 18:38

1 Answer 1

1

This gets a holiday array with just days.month

if (data.response) { //response from social network
    var f_cnt = data.response.length;
    for(var i=0;i<f_cnt;i++) { 
        holidaysArr.push(data.response[i].bdate); //pushing date in array
        //modify in loop? or call function to modify later?
    }
          holidaysArr = (holidasArr.join(',')).match(/\d+\.\d+/g);
    holidaysLoaded=true; // just flag
}

EDIT Sorry the regex was wrong; it's been modified with correct regex


EDIT Here's a JSFiddle Link


EDIT Updated the JSFiddle Link

Sign up to request clarification or add additional context in comments.

3 Comments

np...the jsfiddle link I had didn't initially have sample .year data in it, but the new one does (both links point to the new one, now)
@966p -- also, please mark as solved when you get the chance :)
I updated the structure, check the UPDATE in the mainpost please :s

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.