Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  92] [ 5]  / answers: 1 / hits: 29423  / 9 Years ago, mon, january 18, 2016, 12:00:00

I've got an array with multiple objects in it with the following information:



string ProductDescription/Name
int Amount


I want to create an new array that combines the Amount of the same productdescriptions. Example: the 1st array contains: Product X 50, Product X 80 and Product X 140.



My new array should be Product X 270.
Now I found something about checking if a value is already in an array: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some



But I want something that will check the ProductDescription, instead of the object.



So I took the same function:



function checkAvailability(arr, val) 
{
return arr.some(function(arrVal)
{
return val === arrVal;
});
}


And I try it like this:



var found = checkAvailability(arrayProduct['Productomschrijving'], javascript_array[i]['Productomschrijving']);


But it gives an error...




Uncaught TypeError: Cannot read property 'some' of undefined




When I do it like this:



var found = checkAvailability(arrayProduct, javascript_array[i]['Productomschrijving']);


It's always false. Kinda normal since it's comparing a string with objects.



So how can I get this solved that my some function is checking on the property of an object instead of the complete object?


More From » arrays

 Answers
14

Correct comparison should be val with arrVal.Productomschrijving:



function checkAvailability(arr, val) {
return arr.some(function(arrVal) {
return val === arrVal.Productomschrijving;
});
}

[#63673] Saturday, January 16, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ariel

Total Points: 523
Total Questions: 111
Total Answers: 100

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;