1

I want to store the output in an array and remove the duplicates

 $(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "cview.xml",
        dataType: "xml",
        complete: function(data, status) {
            parseXml(data.responseXML);
        }
    });
});

function parseXml(xml) {
    $(xml).find("containmentView").each(function() {

        var tabarr = $(this).attr("type");
        alert(tabarr);


    });

}

XML feed

 <?xml version="1.0" encoding="utf-8"?>
        <entry>
        ----
        ----
        <cView type="D1">
                        <field name="TargetObjectClass">Disk</field>
                        <field name="TargetObjectName">DISK A1</field>
                        <field name="DisplayName">DISK-Name</field>
                        <field name="MaxAvgDataRate KB/sec">50.00 KB/sec</field>
                        <field name="MaxAvgQueueDepth">50.00</field>
                    </cView>
                    <cView type="D2">
                        <field name="TargetObjectClass">Disk</field>
                        <field name="TargetObjectName">DISK B2</field>
                        <field name="DisplayName"> Disk-Name 2 </field>
                        <field name="MaxAvgDataRate KB/sec">60.00 KB/sec</field>
                        <field name="MaxAvgQueueDepth">60.00</field>
                    </cView>


        ...
        </entry>
frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
user244394
  • 13,168
  • 24
  • 81
  • 138
  • 1
    Maybe you could just form your array, then use [this function](http://stackoverflow.com/a/1961068/145346) to only leave unique data. – Mottie Feb 20 '12 at 03:43

2 Answers2

7

In ExtJS you could use the unique function:

var uniqueArray = Ext.Array.unique(duplicatesArray);

It seems like you are mainly using jQuery though, if you were to ExtJSify your code the operation would be something like this:

var uniqueArray = []

Ext.define('myModel', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'TargetObjectClass', type: 'string'},
        {name: 'TargetObjectName', type: 'string'},
        {name: 'DisplayName', type: 'string'},
        {name: 'MaxAvgDataRate KB/sec', type: 'string'},
        {name: 'MaxAvgQueueDepth', type: 'numeric'}
    ]
});

var myStore = Ext.create('Ext.data.Store', {
    model: myModel,
    proxy: {
        type: 'ajax', 
        url: 'cview.xml',
        reader: 'xml'
    },
    autoLoad: true,
    listeners: {
        load: function(store) {
            uniqueArray = Ext.Array.unique(store.getRange());
        }
    }
});

The records would also get each of the Ext.Model properties so they can be sorted, etc.

egerardus
  • 11,316
  • 12
  • 80
  • 123
  • Thanks . I am trying to create a extjs table . Create unique tabs based on "type" value, and then create tables related to the tab. how would i define field name for the model is the "name" values are dynamic? – user244394 Feb 20 '12 at 15:57
  • 1
    If the data columns are expected to change and you don't want to use server side code, it may be easier to use the other ExtJS method for doing Ajax calls Ext.Ajax. [Here it is in the docs.](http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Ajax) Then you can just iterate through the results of the Ajax and create the objects you need based on what it returns. – egerardus Feb 20 '12 at 22:47
1

You can use Array.prototype.filter and Array.prototype.indexOf:

function removeDuplicatesFromArray (duplicatesArray) {
    if (!Array.isArray(duplicatesArray)) {
        return undefined;
    }
    var uniqueArray = duplicatesArray.filter(function(elem, pos) {
        return duplicatesArray.indexOf(elem) === pos;
    });
    return uniqueArray;
};
dimas
  • 379
  • 2
  • 5