jQuery remove last element from array on back button [duplicate] - javascript

This question already has answers here:
Remove last item from array
(28 answers)
Closed 6 years ago.
Trying to remove the last element from an array when a back button is clicked. Console is showing the correct elements in the array. it's looking like the array.slice function isn't working however I can't see why.
Code is:
$('#backButton .back').click(function(e) {
e.preventDefault();
answers.slice(0,-1);
console.log(answers);
});
Answers array is showing the correct result apart from the last element in the array isn't being removed. Thanks!

The slice() method just returns the portion of an array it will not update the original array. You can use splice(-1, 1) or pop() method to remove the last element from array.
$('#backButton .back').click(function(e) {
e.preventDefault();
answers.pop();
console.log(answers);
});

Related

Filter Out Element from Nested Array [duplicate]

This question already has answers here:
Remove all falsy values from an array
(26 answers)
How to remove false values from array?
(6 answers)
Closed 3 days ago.
let input array=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let output array=[["1.81","2.24"],["5.62","6.26"],["2.31","1.64"]];
I have a nested input array which contains smaller arrays and false statement as shown in the console. How do I remove the false statement from the input array? I have tried using for loop to loop through all the 6 elements to check each element with a (if !==false), then push into a new array called the output array but I could not get it to work? May I know how to solve this? Your help will be very much appreciated :)
Directly use Array#filter:
let input=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let res = input.filter(Boolean);
console.log(res);

How to get the one before last element of an array in JavaScript? [duplicate]

This question already has answers here:
get the second to last item of an array?
(11 answers)
Closed 10 months ago.
I wanna to get one before last element of array in JavaScript. but in console, when I use this code "array.length -2"dose not work, and it just return me the length of array minus 2.
and when I use this code "array.at[-2]" it returns "undefined"!
const arr = [1,2,3,4,5,6]
console.log(arr[arr.length -2])
//5

Filter Array if elements are in another Array [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 1 year ago.
Trying to figure out the proper syntax on filtering elements out of one array if they belong in another but can't get it right. In the following example I'm trying to remove any elements in array1 that are in array2. Any ideas on how to make it work?
array1.filter( element => array2.includes( element ) );
array1.filter( element => !array2.includes( element ) );

Iteration in javascript only returns the first element [duplicate]

This question already has answers here:
jQuery ID selector works only for the first element
(7 answers)
Closed 2 years ago.
I have the following code snippet
$("#personalizacoesOpcionais")
.find(".qtdPersonalizacao")
.each(function (n, t) {
var i = {};
i.IdProdutoIngrediente = parseInt($(t).attr("data-id"));
i.Qtde = parseFloat($(t).text());
r[n] = i
});
In the html there are several divs personalizacoesOpcionais and each one with numerous elements qtdPersonalizacao.
However, the code snippet above only returns the first item qtdPersonalizacao within the first personalizacoesOpcionais.
However, the code snippet above only returns the first item qtdPersonalizacao within the first personalizacoesOpcionais.
Ids are supposed to be unique within a document and jQuery will just returns the first matching element.
The recommended solution would be to use a class instead of an id for personalizacoesOpcionais.
If this is not possible, for example if you don't control the code that generate the markup, a workaround would be to use $("div[id=personalizacoesOpcionais]") instead of $("#personalizacoesOpcionais")

Delete element in associative array [duplicate]

This question already has answers here:
How do I unset an element in an array in javascript?
(8 answers)
Closed 5 years ago.
I'm trying to create a button so that when clicked, a specific element stored in an associative array will get deleted. But what I've done doesn't seem to work. Any help will be appreciated.
// Creates the button
bodyText = bodyText + '<input type="button" id="btnDeleteQuestion"
value="Delete a question">';
document.getElementById("btnDeleteQuestion")
.addEventListener("click", function() {
// Deletes the third question stored in the questionBank
delete questionBank[2]['questionText'];
console.log(questionBank);
});
}
Use splice (if it is an array) to remove the element from the array.
questionBank.splice(2, 1); // from the third element, remove 1 item.
What you have written in your question will get the questionBank[2] and then delete a property called questionText in the object.
It will also throw an error if the array does not have at least 3 items or if the 3rd item is null or undefined
I hope you helpful this
var questionBank= ["questionText", "questionText1", "questionText2"];
document.getElementById("btnDeleteQuestion").addEventListener("click", function() {
// index get by text
const index = questionBank.indexOf("questionText");
if(index!=-1){
//Delete element by index
questionBank.splice(index, 1);
}
console.log(questionBank);
});

Categories

Resources