我想将输出存储在数组中并删除重复项
$(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>
答案 0 :(得分:6)
在ExtJS中,您可以使用unique功能:
var uniqueArray = Ext.Array.unique(duplicatesArray);
看起来你主要使用的是jQuery,如果你使用ExtJSify你的代码,操作将是这样的:
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());
}
}
});
记录还将获取每个Ext.Model属性,以便对它们进行排序等。
答案 1 :(得分:1)
您可以使用Array.prototype.filter
和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;
};