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

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]
                            });
                     
                    }
               }

Hi,

Welcome to the forums :slight_smile:

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:

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.

Thanks Pullo :slight_smile:

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

No probs.

What does data contain?

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

Ok, so data contains an array of objects(?) like this:

[
  {
      "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.

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…

This is the preview of what i have

Ok, so based on the data from post six, what would you like the tabs array to contain?
An example would be helpful:

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

In post 1 you are trying to push data into an array called tabs:

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:

[
  {
      "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.

dayNo will be tabs heading and Theme, time,Topic will the tabs content according to dayNo

You are not understanding me :slight_smile:

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

tabs = [Day 1, Day2, Day3, etc…]

But what should Day 1, Day2 etc contain?
Please give examples based on this data:

[
  {
      "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"
  }
  ...
]

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:

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
}

Hi fretburner,
I have the same issue like this
http://stackoverflow.com/questions/13486479/how-to-get-an-array-of-unique-values-from-an-array-containing-duplicates-in-java

So, a new day and a new attempt to understand your issue :slight_smile:

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:

[
  {"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:

[
  {
    "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).