SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can i skip repeated value in an Array

    Hi,

    I have a jquery.ajax success function like below, in which i am getting repeated values for "text", How can i skip the repeated value from this array.. I am getting the values now like this text = Day : 1, Day : 2, Day : 2, Day : 3, Day : 3, Day : 4, Day : 5, etc...How can i skip these repeated values from this. Thanks in Advance

    Code:
    success: function (data) {
                         var tabs = [];
                        for (i = 0; i < data.length; i++) {
                           
                            var rows = data[i];
    
                            tabs.push({
                                    dayNo: rows[2],
                                    text: "Day : " + rows[2]
                                });
                         
                        }
                   }
    Last edited by Pullo; Feb 12, 2014 at 02:28. Reason: Added code tags

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Welcome to the forums

    You might consider doing this on the server and only return what you need to your JS.

    If that's not an option, then could you change your success callback thus:

    Code:
    success: function (data) {
      console.log(data);
    }
    and post the output.

    Also, please let us know what you would like the content of the tabs array to be.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Pullo

    I am getting rows[2] values like 1,2,2,3,3,3,4,4.5 etc... So i don't need to get these repeated values in rows[2], Can i use any for loop to skip this repeated values ? Thanks

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    No probs.

    What does data contain?

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Below the contents in data Where id,event_id,dayNo,Theme,time,Topic respectively. Here dayNo has 2 Topics, So want to take that particular topic from that column, But no need to take the dayNo again since it will be there already one.

    1,4,1,theme 4,08:00 AM,Registration
    2,4,2,theme 4,08:30 AM,Speaker 1
    3,4,2,Theme 3,08:00 AM,Speaker 2

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Ok, so data contains an array of objects(?) like this:

    Code:
    [
      {
          "id": 1,
          "event_id": 4,
          "dayNo": 1,
          "Theme": "theme4",
          "time": "08: 00AM",
          "Topic": "Registration"
      },
      {
          "id": 2,
          "event_id": 4,
          "dayNo": 2,
          "Theme": "theme4",
          "time": "08: 30AM",
          "Topic": "Speaker1"
      },
      {
          "id": 3,
          "event_id": 4,
          "dayNo": 2,
          "Theme": "theme3",
          "time": "08: 00AM",
          "Topic": "Speaker2"
      }
      ...
    ]
    Correct?

    What do you want the contents of the tabs array to be?
    Please give us an example.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea, Correct. I have tabs and it's contents, Tabs heading will be dayNo and each tabs contents will be it's topics... eg: my first tab will be day : 1 and it's content will be theme4, 08: 00AM, Registration according to the above example. Like this i am getting 2 tabs for dayNo 2 , because there is 2 Topics for dayNo 2 . I don't want to repeat this dayNo again, But i want that content. I am getting the content properly, But the problem with dayNo..

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Preview.jpg This is the preview of what i have

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Ok, so based on the data from post six, what would you like the tabs array to contain?
    An example would be helpful:

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Preview.jpg As i show the attached picture, dayNo are repeating Day : 2, I don't want to repeat that... If dayNo are repeating it should be skip

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    In post 1 you are trying to push data into an array called tabs:

    Code:
    success: function (data) {
                         var tabs = [];
                        for (i = 0; i < data.length; i++) {
                           
                            var rows = data[i];
    
                            tabs.push({
                                    dayNo: rows[2],
                                    text: "Day : " + rows[2]
                                });
                         
                        }
                   }
    In post 7 you agree that data contains something like this:

    Code:
    [
      {
          "id": 1,
          "event_id": 4,
          "dayNo": 1,
          "Theme": "theme4",
          "time": "08: 00AM",
          "Topic": "Registration"
      },
      {
          "id": 2,
          "event_id": 4,
          "dayNo": 2,
          "Theme": "theme4",
          "time": "08: 30AM",
          "Topic": "Speaker1"
      },
      {
          "id": 3,
          "event_id": 4,
          "dayNo": 2,
          "Theme": "theme3",
          "time": "08: 00AM",
          "Topic": "Speaker2"
      }
      ...
    ]
    Therefore, in your success callback, given that data contains the above, what would you like the tabs array to contain?

    Please don't post a screen shot.

  12. #12
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dayNo will be tabs heading and Theme, time,Topic will the tabs content according to dayNo

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    You are not understanding me

    Code:
    tabs = [ what data do you want here??? ]

  14. #14
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tabs = [Day 1, Day2, Day3, etc...]

  15. #15
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    But what should Day 1, Day2 etc contain?
    Please give examples based on this data:

    Code:
    [
      {
          "id": 1,
          "event_id": 4,
          "dayNo": 1,
          "Theme": "theme4",
          "time": "08: 00AM",
          "Topic": "Registration"
      },
      {
          "id": 2,
          "event_id": 4,
          "dayNo": 2,
          "Theme": "theme4",
          "time": "08: 30AM",
          "Topic": "Speaker1"
      },
      {
          "id": 3,
          "event_id": 4,
          "dayNo": 2,
          "Theme": "theme3",
          "time": "08: 00AM",
          "Topic": "Speaker2"
      }
      ...
    ]

  16. #16
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,412
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi salimali, Pullo,

    Based on information in previous posts, I've made two assumptions:

    1. The data variable is an array of arrays, not an array of objects.
    2. The desired output is a single tab for each day, with the tab contents showing a list of the events for that day (theme, time, topic).

    If those two assumptions are correct, this code should do what you want:

    Code JavaScript:
    success: function (data) {
        var tabs = [],
            days = [];
     
        // For each row of data, add the event to an array, indexed by the day
        $.each(data, function(index, row) {
            var i = row[2]-1;
            if (days[i] === undefined) {
                days[i] = [];
            }
            days[i].push(row.slice(3).join(' '));  // Creates an event string by joining the last 3 array elements
        });
     
        // Loop over the days array, creating a new tab for each day
        $.each(days, function(index, day) {
            tabs.push({ dayNo: index+1, text: day.join('<br>') });  // Join the current day's events into a single string, separated by <br> elements
        });
     
        // Do something with tabs array
    }

  17. #17
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fretburner,
    I have the same issue like this
    http://stackoverflow.com/questions/1...icates-in-java

  18. #18
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    So, a new day and a new attempt to understand your issue

    It seems:

    You have an ajax call that returns some data.
    You want to manipulate that data.
    You want to use the manipulated data to build a bunch of tabs containing event details.

    What we need to know from you is the format you would like to manipulate the data into.
    Let's take a simplified example:

    Returned ajax data is:
    Code:
    [
      {"day": "Mon", "Event": "seminar", "time": "9:00"},
      {"day": "Mon", "Event": "lecture", "time": "11:00"},
      {"day": "Tue", "Event": "meeting", "time": "13:00"},
      {"day": "Tue", "Event": "colloqium", "time": "19:00"}
    ]
    You want to turn it into:

    Code:
    [
      {
        "Mon": {
          {"Event": "seminar", "time": "9:00"}, 
          {"Event": "lecture", "time": "11:00"},
        }
      },
      {
        "Tue": {
          {"Event": "meeting", "time": "13:00"}, 
          {"Event": "colloquium", "time": "19:00"},
        }
      }
    ]
    It is this second step we want to know from you.
    We need to know the format of the manipulated data (which you can best explain by using an example like above).


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •