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.
Related
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How to remove item from array by value? [duplicate]
(37 answers)
Closed 2 months ago.
I have an array which has some empty items.
const array = ["a","","c","","e","f","g"]
I am trying to eliminate empty items and leave only the items having string. I mean manipulating the array as:
array = ["a","c","e","f","g"]
There are many alternative like array.map, array.filter, array.slice, array.splice.. What is the most costless and recommended way of doing this?
As far as I know most cost effective way is to use the array filter method:
const array = ["a","","c","","e","f","g"];
const results = array.filter(element => {
return element !== '';
});
console.log(results);
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);
}
});
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);
});
This question already has answers here:
Selecting last element in JavaScript array [duplicate]
(13 answers)
Closed 6 years ago.
So say I have an array and the user can add do it by a input html tag:
var example=["a","b"];
var command=document.getElementByID("id");
so idk how long the array will be when i execute the next step,which is selecting the last item in the array and register it in an object
example.split(",")
someObject[//how do i chose the last item?]
You can select the last item in a javascript array like this
arr = example.split(",");
lastArr = arr[arr.length - 1];
This question already has answers here:
Meteor - #each iteration of an array with another HTML element inserted after each nth item
(2 answers)
Closed 6 years ago.
I'm quite new to meteor blaze. I want to know how it's possible to display something (an advert) after a particular number of iteration in meteorjs. Any ideas? Thanks in advance.
And what about doing this within the js helper? I did it in this way.
Template.foo.helpers({
stuffWithAds : function(){
col = GivenCollection.find().fetch();
toTemplate = [];
for(i=0; i<col.length; i++){
toTemplate.push(col[i]);
if (i % 2 != 0){
toTemplate.push('whatever you want to insert');
}
}
return toTemplate; }
});
This inserts whatever you want after 2 elements.