Use of unique() when data is an object

Use of unique() when data is an object

darrenmdarrenm Posts: 18Questions: 8Answers: 0

I have a table that is loaded from JSON and for one of the columns the JSON is an object rather than a primitive type eg

{
    "data": [{
        "column1": "30027215",
        "column2": {
            "display": "Some display text",
            "sortvalue": "123"
        }
    }
]}

When i apply the unique() function to column 2, I get one result for each row, regardless if there are duplicate values in columns.display. I understand why this is but is there a way of getting unique() to operate on a specific data attribute in the object, or do I need to write my own unique function?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 6,896Questions: 0Answers: 1,179

    It seems to be working as expected here. Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.

    Cheers,

    Colin

  • darrenmdarrenm Posts: 18Questions: 8Answers: 0

    I've updated your example here: http://live.datatables.net/gisesalu/2/edit (thanks for that I was trying to work out how to demo the Ajax load with local JSON).

    The main difference is that I am specifying the object, not the attribute, as the column data. This is because I then use render to do different things based on various criteria. So unique() is working with an object, not a primitive type. It therefore doesn't know how to tell if a value is unique.

    If it was possible to do something like unique(data.display) so that it knew to look at that attribute, it would be great. If not, I'll have to build an array and reduce and sort it myself. Which is no big deal, but I thought there might have been a way.

  • colincolin Posts: 6,896Questions: 0Answers: 1,179
    Answer ✓

    Yep, if it's the object, then each object is unique, so as you say it will return them all. If possible, it would be best to define the column as I did, rather than in the render.

    Colin

  • darrenmdarrenm Posts: 18Questions: 8Answers: 0

    Unfortunately can't do that because the data that goes in the table cell is determined at run time based on some local conditions.

    I'll just have to build my own array. It's to create a <select> for table filtering. I suppose I could traverse the select list after it has been built and remove duplicates. Anyway thanks for taking a look.

Sign In or Register to comment.