I'm trying to remove the first element from array which I getting from xml file. I tried to use splice method but it doesn't work. Can someone help me?
.ajax({
type: "GET",
url: 'my.xml',
dataType: "xml",
success: function(xml) {
var array = [];
var data = $.xml2json(xml)['#document'];
that.array = data.contacts;
}
})
data:
-
3can you share the output json and what you want to delete from it?gurvinder372– gurvinder3722016年03月31日 10:16:42 +00:00Commented Mar 31, 2016 at 10:16
-
1couldn't see where you're using splice method...Bhojendra Rauniyar– Bhojendra Rauniyar2016年03月31日 10:18:34 +00:00Commented Mar 31, 2016 at 10:18
-
here is my array how looks like link I'd like to delete element with index 0Christian– Christian2016年03月31日 10:21:48 +00:00Commented Mar 31, 2016 at 10:21
-
@Christian which part do you want to remove?gurvinder372– gurvinder3722016年03月31日 10:28:43 +00:00Commented Mar 31, 2016 at 10:28
2 Answers 2
As you have attached a screenshot of your Object data then you can use Array.prototype.shift() to remove the first entry in the array:
var array = [];
var data = $.xml2json(xml)['#document'];
array = data.contact.name.shift(); // <----this will remove the first entry in the array.
a sample demo:
var array = [];
var data = {
contact: {
name: [{
name: "one"
}, {
name: "two"
}, {
name: "three"
}]
}
};
array = data.contact.name.shift(); // <----this will remove the first entry in the array.
document.querySelector('pre').innerHTML = JSON.stringify(data, 0, 3);
<pre></pre>
answered Mar 31, 2016 at 10:23
Jai
74.8k12 gold badges77 silver badges104 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Find the index of the element you want to remove (using indexOf) and then use splice to remove it....
var idx = that.array.indexOf(theIndexyouWantToRemove);
that.array.splice(idx, 1);
If its definitely the first element always, then you could use shift().
answered Mar 31, 2016 at 10:23
Stuart
6,7692 gold badges29 silver badges42 bronze badges
1 Comment
Stuart
Pleasure. Tick as correct if it answers your question :)
lang-js