1

I have array like this:

var notes = ["user1,date:13/2/2008,note:blablabla", "user1,date:15/2/2008,note:blablabla", "user1,date:17/2/2008,note:blablabla", "user1,date:13/3/2008,note:blablabla"];

And I have

var search_date="17/2/2008";

I want to find last occurence of note and user for that note. Anyone knows how? Thanks in advance for your reply.

Marko Vasic
  • 690
  • 9
  • 27

7 Answers7

1

You can iterate the array and check the attribute

or

you can user underscore.js: http://underscorejs.org/#filter

Balint Bako
  • 2,500
  • 1
  • 14
  • 13
1

Try this:

var highestIndex = 0;
for (var i = 0; i < notes.length; i++){
    if (notes[i].indexOf(search_date) != -1){
        highestIndex = i;
    }
}
//after for loop, highestIndex contains the last index containing the search date.

Then to get the user, you can parse like this:

var user = notes[highestIndex].substring(0, notes[highestIndex].indexOf(',') - 1);
p e p
  • 6,593
  • 2
  • 23
  • 32
  • Just be aware that indexOf isn't supported in IE8 and older. - http://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8 – Joel Cochran Jul 05 '13 at 18:12
  • Joel - thank you, but now this raises a question and I'm not 100% clear on the answer. I've read (for example, http://stackoverflow.com/questions/6829101/old-ie-javascript-doesnt-support-indexof) that indexOf on String (how I am using it) has existed since very early IE versions. I have read that indexOf support for Arrays does not exist for IE8 and below. I can't give a definitive answer on that without trying it, but it sounds like that's the case from other sources as well. – p e p Jul 05 '13 at 18:29
  • 1
    Oops - my mistake, I didn't read closely enough. Array indexOf is missing, but yes String's indexOf should work fine. – Joel Cochran Jul 05 '13 at 18:34
  • I know that there are similar solutions to this, but I marked this as answer because I used this solution. Thanks everyone for help. – Marko Vasic Jul 08 '13 at 07:40
1
for (var i = 0; i < notes; i++) {
    if (notes[i].indexOf(search_date) != -1) {
        // notes [i] contain your date
    }
}
Reason
  • 1,410
  • 12
  • 33
1
var match = JSON.stringify(notes).match("\"([^,]*),date\:"+search_date+",note\:([^,]*)\"");
alert(match[1]);
alert(match[2]);

works ;-)

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62
0

Something like this:

var notes = ["user1,date:13/2/2008,note:blablabla", "user1,date:15/2/2008,note:blablabla", "user1,date:17/2/2008,note:blablabla", "user1,date:13/3/2008,note:blablabla"];
var search_date="17/2/2008";

var res = [];

for(var i = 0; i < notes.length; i++) {
  var note = notes[i];
  if(note.indexOf(search_date) !== -1) {
    res.push(note.substring(note.indexOf('note:') + 1), note.length);
  }
}

var noteYouWanted = res[res.length - 1];
0

For the last occurrence and if performance matters:

var notes = ['user1,date:13/2/2008,note:blablabla', 'user1,date:15/2/2008,note:blablabla', 'user1,date:17/2/2008,note:blablabla', 'user1,date:13/3/2008,note:blablabla'],
    search = '17/2/2008',
    notesLength = notes.length - 1,
    counter,
    highestIndex = null;

for (counter = notesLength; counter >= 0; counter--) {
    if (notes[counter].indexOf(search) !== -1) {
        highestIndex = counter;
        break;
    }
}

// do something with notes[highestIndex]
Jon
  • 1,234
  • 2
  • 12
  • 30
0
var notes = ["user1,date:13/2/2008,note:blablabla", "user1,date:15/2/2008,note:blablabla", "user1,date:17/2/2008,note:blablabla", "user1,date:13/3/2008,note:blablabla"];

var search_date="17/2/2008";
var user, note;

$.each(notes, function(i) {
    var search = new RegExp('\\b' + search_date + '\\b','i');
    // if search term is found
    if (notes[i].match(search)) {
      var arr = notes[i].split(',');
      user = arr[0];
      note = arr[2].substr(5);
    }
}); // end loop

console.log(user);
console.log(note);

example here: http://jsfiddle.net/Misiu/Wn7Rw/

Misiu
  • 4,738
  • 21
  • 94
  • 198
  • @MrCkobe - click edit on top right, or use this link: http://jsbin.com/epexil/3/edit then on top click run with js – Misiu Jul 05 '13 at 15:00