Delete element in associative array [duplicate] - javascript

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);
});

Related

how to push object into an nested array within a loop? [duplicate]

This question already has answers here:
Push is overwriting previous data in array
(2 answers)
Closed 2 years ago.
I am using an angularjs foreach and want to find data objects to load into another objects array.
When doing this, it seems like I am adding the variable and all the added objects change to the last variable values.
var p = {};
angular.forEach(d, function(personnel, index){
if(wo.doc.job === personnel.job){
p["EmployeeID"] = personnel.EmployeeID;
p["Number"] = personnel.Number;
wo.doc.personnel.push(p);
console.log(personnel);
}
});
If this finds to 2 employees for a job, they are added and as i watch the wo.doc object after the second object is added the 2 added objects are the same as the last object.
Make a new object in the loop.
angular.forEach(d, function(personnel, index){
if(wo.doc.WorkOrderDetailID === personnel.PrimeWorkOrderNum){
var p = {EmployeeID: personnel.EmployeeID, Number: personnel.Number};
wo.doc.personnel.push(p);
}
});

Add/Remove item from array - javascript [duplicate]

This question already has answers here:
javascript remove item from array, if an item already existing in array
(6 answers)
Closed 5 years ago.
I was wondering how I can add/remove item to array depending on if it's already in there.
Basically, I have a list of items and every item has their own ID, I want to add it or remove it depending on if it's already in the basket array and indicate (with css) if it's already in there.
Thanks in advance.
You can use jQuery built in function $.inArray
cart = ["value1","value2","value3","value4"]; // Cart Items
var new-item = "value2";
var index = $.inArray(new-item, cart);
if(index == -1){
// Item doesn't exist. Add this item to the cart
cart.push(new-item);
}else{
//Item already exists.
//Delete that item.
unset($array[index]);
}
console.log(cart); //Display the cart items.

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

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);
});

Loop through Drop Down Option and read its attribute using jquery [duplicate]

This question already has answers here:
How to get the attributes of a HTML element using JQuery?
(4 answers)
Closed 8 years ago.
I am looping through drop down option and checking attribute.
if attribute match than counter is increase. At the end i show counter as alert.
This is my code but some how its not working dont know why
var count= 0;
$('.mydropdown option').each(function () {
var level = this.attr("myattr");
if (level == "0") {
count++;
}
});
alert(count);
}
this is a plain javascript object, it does not contain a function called .attr()
Try,
var level = $(this).attr("myattr");
Just convert the this reference into a jquery object and invoke .attr() over it

.find('.foo').text() as array [duplicate]

This question already has answers here:
Get array of text values using selector instead of iterating?
(7 answers)
Closed 9 years ago.
I have html with multiple things classed .text. I the perform
console.log($(whatever).find('.foo').text());
The result is a textstring with all the contents in it. Can I save it to an array instead somehow?
You can use the map method to get this done
var textArray = $(whatever).find('.foo').map(function() {
return $(this).text();
}).get();
This will get you an array.. If you want to display it as a string then just use join along with it.
Check Fiddle
var arr = [];
$(whatever).find('.foo').each(function() {
arr.push($(this).text());
});

Categories

Resources