对象属性的javascript .some函数 [英] javascript .some function for object property
问题描述
我有一个包含多个对象的数组,其中包含以下信息:
I've got an array with multiple objects in it with the following information:
string ProductDescription/Name
int Amount
我想创建一个新数组,该数组合并了相同产品描述的金额。示例:第一个数组包含:产品X 50,产品X 80和产品X 140。
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.
我的新数组应为产品X270。
现在我找到了关于检查值是否已经在数组中的一些信息: https ://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some
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
但是我想要一些可以检查的东西 ProductDescription
而不是对象。
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;
});
}
我尝试这样:
var found = checkAvailability(arrayProduct['Productomschrijving'], javascript_array[i]['Productomschrijving']);
但是它给出了一个错误...
But it gives an error...
未捕获的TypeError:无法读取未定义的属性 some
Uncaught TypeError: Cannot read property 'some' of undefined
当我这样做时:
var found = checkAvailability(arrayProduct, javascript_array[i]['Productomschrijving']);
总是错误的。
所以我该如何解决我的 some
函数是检查对象的属性而不是完整的对象?
So how can I get this solved that my some
function is checking on the property of an object instead of the complete object?
推荐答案
正确的比较应该是 val
与 arrVal.Productomschrijving
:
function checkAvailability(arr, val) {
return arr.some(function(arrVal) {
return val === arrVal.Productomschrijving;
});
}
这篇关于对象属性的javascript .some函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!