Answer the question
In order to leave comments, you need to log in
How to find if an element is in an array?
Basics of the basics. I'm adding to cart
var ProductsList = [],
Cart = function (id, name) {
this.id = id;
this.name = name;
};
function addProduct(id, name) {
var newItem = new Cart(id, name);
ProductList.push(newItem);
}
angular.forEach(Cart, function(id) {
// и вот здесь понять не могу как сравнить приходящий id с Cart.id
}
Answer the question
In order to leave comments, you need to log in
var ProductsList = [],
Cart = function (id, name) {
this.id = id;
this.name = name;
}
;
function addProduct(id, name) {
ProductsList.some(function(e){
return e.id == id;
})||ProductsList.push(new Cart(id, name));
}
Insert not into an array, but into another object, like this { id: {name: name} }, and the check for existence in the basket will solve itself
According to the angular.forEach documentation , iterating over the elements should look like this for your example.
angular.forEach(ProductsList, function(value, key) {
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question