This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 3 years ago.
My code :
<html>
<body>
<p id='demo'></p>
<script>
var array=['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
delete array[2];
document.getElementById('demo').innerHTML = array;
</script>
</body>
</html>
When I running this code...
The output
Comma quotation were not deleted after execution.
Help me to solve this (or) Suggest any other ways for deleting values in the specified index in js array.
var a = ['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
a.splice(2,1);
console.log(a);
Change
delete array[2]
to
array.splice(2,1)
Regards.
I would say use:
array.splice(index, 1);
the "index" parameter is an index of the element you want to remove and "1" is a number of elements you want to remove. splice() will take care of the indexes of removed/added elements for you.
If you don't know the index of the element you can try:
array.indexOf("BMW");
Thanks.
var array=['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
array.pop[2];
By following you can delete the element in array .pop[]
Related
Brief background: I retrieve book info from google books and worldcat via each api. I parse through the data to create an html div outputs with stuff of interest for a webapp.
The problem: the isbns. After getting isbns from each, I merge them into one div. For example:
<span id='tada'>9781137012920 9780230339118 9781137012920 1137012927</span>
I would like to eliminate the duplicate isbn (e.g., 9781137012920) with either javascript or jquery. I can get the text of the span but every solution I have tried has failed thus far.
Any help - esp, with a codepen or fiddle - for this amateur would be greatly appreciated.
PS:I know variations of this question have been addressed on StackOverflow - and believe me that I am sick of duplicates! - but I have not managed to fix this after a couple of days of trying. Examples that type in the content as var work but how do I get it from the div and make it work?
Do something like this using text() method with callback
$('span').text(function(i, v) { // use callback to get old value
return v.split(' ') // split the old value
.filter(function(v1, i, arr) { // v1 is array element, i is index, arr is iterating array
return arr.indexOf(v1) == i; // filter unique value by checking the index
}).join(' ') // join and return the unique values
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='tada'>9781137012920 9780230339118 9781137012920 1137012927</span>
For more about finding unique array element refer : Remove Duplicates from JavaScript Array
I suggest removing the duplicates before making them into the div. If you are getting the values in an array you can eliminate the duplicates easily. Remove Duplicates from JavaScript Array
I think you want something like this :
var string = $('span').text();
var uniqueList=string.split(' ').filter(function(allItems,i,a){
return i==a.indexOf(allItems);
}).join(' ');
$('#output').append(uniqueList);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div>
<h1>Input</h1>
<span>9781137012920 9780230339118 9781137012920 1137012927</span>
</div>
<div id="output">
<h1>Becomes</h1>
</div>
This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 8 years ago.
I am trying to get the first word out of the variable var solution = [cow, pig]
I have tried everything from strings to arrays and I can't get it. Please help.
As per the comments
solution[0]
Will return the first item in the array.
solution[1]
would be the second, or undefined if the array was:
var solution = [cow]
Is solution an array, or is it in that form? (var solution = [cow, pig]) You also need to add quotes around those values, unless those values are defined variables.
You need to change the variable to look like this:
var solution = ['cow', 'pig']
If so, just get the value at subscript 0.
var result = solution[0];
console.log(result);
If you mean an string like
solution = "cow pig".
Do
solution = solution.split(' ')[0];
console.log(solution); //Will return cow
This question already has answers here:
Selecting the first "n" items with jQuery
(6 answers)
Closed 9 years ago.
I am selecting all tags where href attribute is starting with "picThumbImgA_" as below.
$('a[id^="picThumbImgA_"])')
now i want to update some attributes of first four tag. I can select first as
$('a[id^="picThumbImgA_"]):first')
and last as $('a[id^="picThumbImgA_"]):last')
odd as $('a[id^="picThumbImgA_"]):odd')
and even as $('a[id^="picThumbImgA_"]):even')
But how can I process first four tags only.
Please help me out for the same.
You can try the Zero-based index selector :lt(), like :
$('a[id^="picThumbImgA_"]):lt(4)')
From jQuery's documentation:
Select all elements at an index less than index within the matched set.
You could also use :nth-child(), like:
$('a[id^="picThumbImgA_"]):nth-child(-n+4)')
You can use the jQuery each() function and exit the function by return false when you have processed the first 4.
$('a[id^="picThumbImgA_"])').each(function(index, el) {
if(index < 4) {
//do something
} else {
return false;
}
});
This question already has answers here:
Why do multiple `.appendTo` calls on a newly created jQuery element only append it once?
(2 answers)
Closed 3 months ago.
I'm new here and I'd like to ask a question about jQuery.
i have some problem with jquery each, and i have code :
$("#search-submit").click(function(){
var item = $("<span>asdf</span>");
$("body").append(item, item);
}
I just can't figure out, why is this simple code don't work. Instead of print "asdf" twice, i just got one "asdf" printed each time i click the button..
this also don't work. this is so silly...
$("body").append(item);
$("body").append(item);
Thanks a lot... and sorry about my bad english
Denny.
You can append a jQuery object only one time, you can clone the object:
$("#search-submit").click(function() {
var item = $("<span>asdf</span>");
$("body").append(item, item.clone());
})
Or append a string:
$("#search-submit").click(function() {
var item = "<span>asdf</span>";
$("body").append(item, item);
})
http://jsfiddle.net/yKyAL/
You can use clone
$("#search-submit").click(function() {
var item = $("<span>asdf</span>");
$("#test").append(item.clone(), item.clone());
});
Check the fiddle here
I am wanting to store some info in array
i declared an array
var added = [];
Question: I want to add a number/string at the last of the array.
And then i want to add all the data to the a dom ?
I am new to js array please help me out with this basic things
added.push("123");
added.push("abc");
$('div').html(added.join());
This will set the html of the div to 123,abc
added.push("123");
added.push("abc");
added.push("xyz");
for(i in added)
{
$("div").append(added[i]);
}
Hope this helps!
EDIT:
I though you need to assign all one by one!
You could use:
$("div").html(added.join(', '));
to get things seperated by comma!