我有一个对象数组。所有这些对象不一定具有相同的属性。
如果数组中的某些对象确实具有公共属性,我需要找到它们的总和。
[{"taxAmount":572.26,"totalAmount":2004,"totalDiscount":0,"totalItems":12,"gTotal":2576.2599999999998,"count":4,"deployment":{"_id":"554c92f979d26026307a5a9b","name":"Punjabi Bagh"}},
{"taxAmount":304.9,"totalAmount":1111,"totalDiscount":0,"totalItems":9,"gTotal":1415.8999999999999,"count":2,"deployment{"_id":"554c930579d26026307a5a9c","name":"Pitampura"}},
{"deployment":{"_id":"554c930c79d26026307a5a9d","name":"Model Town"}},
{"deployment":{"_id":"554c945079d26026307a5cae","name":"Dwarka"}},
{"deployment":{"_id":"554cd66c0a40ccd2416e38e7","name":"Green Park"}}]
在上面的例子中,我需要添加计算TaxAmount
和totalAmount
的个别总和。
另外,我需要一些让我们说totalAmount
属性的对象。
我如何在javascript中进行此操作?
另外,有没有一种快速的方法来检查一个对象是否有任何额外的属性,除了我们到目前为止迭代的内容?
答案 0 :(得分:2)
你走在正确的道路上 - 减少是一种方法(你还需要Object.keys
来遍历每个对象的属性
注释内联 - 请注意,在计算/添加
之前,这会执行(简单)isNumber检查var data = [{"taxAmount":572.26,"totalAmount":2004,"totalDiscount":0,"totalItems":12,"gTotal":2576.2599999999998,"count":4,"deployment":{"_id":"554c92f979d26026307a5a9b","name":"Punjabi Bagh"}},
{"taxAmount":304.9,"totalAmount":1111,"totalDiscount":0,"totalItems":9,"gTotal":1415.8999999999999,"count":2,"deployment":{_id:"554c930579d26026307a5a9c","name":"Pitampura"}},
{"deployment":{"_id":"554c930c79d26026307a5a9d","name":"Model Town"}},
{"deployment":{"_id":"554c945079d26026307a5cae","name":"Dwarka"}},
{ "deployment": { "_id": "554cd66c0a40ccd2416e38e7", "name": "Green Park" } }]
var result = data.reduce(function (a, b) {
// loop through the properties
Object.keys(b).forEach(function (key) {
// check if it's a number
if (!isNaN(parseFloat(b[key]))) {
// get the running count / sum for this key or initialize a new one
a[key] = a[key] || { sum: 0, count: 0 }
a[key].sum += Number(b[key]);
a[key].count++;
}
})
return a;
}, {})
console.log(result)
答案 1 :(得分:1)
batchSize
此var obj = [{"taxAmount":572.26,"totalAmount":2004,"totalDiscount":0,"totalItems":12,"gTotal":2576.2599999999998,"count":4,"deployment":{"_id":"554c92f979d26026307a5a9b","name":"Punjabi Bagh"}},
{"taxAmount":304.9,"totalAmount":1111,"totalDiscount":0,"totalItems":9,"gTotal":1415.8999999999999,"count":2,"deployment":{"_id":"554c930579d26026307a5a9c","name":"Pitampura"}},
{"deployment":{"_id":"554c930c79d26026307a5a9d","name":"Model Town"}},
{"deployment":{"_id":"554c945079d26026307a5cae","name":"Dwarka"}},
{"deployment":{"_id":"554cd66c0a40ccd2416e38e7","name":"Green Park"}}],
sumObj = { "taxAmount":0, "totalAmount":0 };
for( var o in obj ){
sumObj.taxAmount += obj[ o ].taxAmount || 0;
sumObj.totalAmount += obj[ o ].totalAmount || 0;
}
测试obj[ o ].taxAmount || 0
是否具有属性obj[ o ]
,以及它是否真实。如果是,则返回,否则返回0.
答案 2 :(得分:1)
假设您的数据包含在名为values
的数组中:
var taxAmount = 0,
totalAmount = 0,
objectsWithTotalAmount = 0;
for (int i = 0; i < values.length; i++) {
if (values[i].hasOwnProperty("taxAmount") {
taxAmount += values[i].taxAmount;
}
if (values[i].hasOwnProperty("totalAmount") {
totalAmount += values[i].totalAmount;
objectsWithTotalAmount++;
}
}
使用.hasOwnProperty()
检查对象是否包含该属性。这样,您就可以获得taxAmount
和totalAmount
的总和,以及具有totalAmount
属性的对象数。
答案 3 :(得分:1)
将prop名称传递给function,检查每个数组实例的每个属性
var testArr = [{"taxAmount":572.26,"totalAmount":2004,"totalDiscount":0,"totalItems":12,"gTotal":2576.2599999999998,"count":4,"deployment":{"_id":"554c92f979d26026307a5a9b","name":"Punjabi Bagh"}},
{"taxAmount":304.9,"totalAmount":1111,"totalDiscount":0,"totalItems":9,"gTotal":1415.8999999999999,"count":2,"deployment":{"_id":"554c930579d26026307a5a9c","name":"Pitampura"}},
{"deployment":{"_id":"554c930c79d26026307a5a9d","name":"Model Town"}},
{"deployment":{"_id":"554c945079d26026307a5cae","name":"Dwarka"}},
{"deployment":{"_id":"554cd66c0a40ccd2416e38e7","name":"Green Park"}}];
var testTotal = getTotal('taxAmount');
alert(testTotal);
function getTotal(propName) {
var total = 0;
for (var i = 0; i < testArr.length; i++) {
for (var key in testArr[i]) {
if (testArr[i].hasOwnProperty(key)) {
if (key === propName) {
total = total + testArr[i][key];
}
}
}
}
return total;
}
&#13;
答案 4 :(得分:0)
不确定你真正想要的是什么。是这个吗?
var obj = [{"taxAmount":572.26,"totalAmount":2004,"totalDiscount":0,"totalItems":12,"gTotal":2576.2599999999998,"count":4,"deployment":{"_id":"554c92f979d26026307a5a9b","name":"Punjabi Bagh"}},
{"taxAmount":304.9,"totalAmount":1111,"totalDiscount":0,"totalItems":9,"gTotal":1415.8999999999999,"count":2,"deployment":{"_id":"554c930579d26026307a5a9c","name":"Pitampura"}},
{"deployment":{"_id":"554c930c79d26026307a5a9d","name":"Model Town"}},
{"deployment":{"_id":"554c945079d26026307a5cae","name":"Dwarka"}},
{"deployment":{"_id":"554cd66c0a40ccd2416e38e7","name":"Green Park"}}],
sumObj = { "taxAmount":[], "totalAmount":[] };
for( var o in obj ){
var current = obj[ o ];
current.taxAmount && sumObj.taxAmount.push( current.taxAmount );
current.totalAmount && sumObj.totalAmount.push( current.totalAmount );
}
答案 5 :(得分:0)
我使用递归函数,它们在Javascript中非常容易使用,只是不断传递相同的变量:
function getSumAllProps(){
var objSums = {};
var arrObjs = [{"taxAmount":572.26,"totalAmount":2004,"totalDiscount":0,"totalItems":12,"gTotal":2576.2599999999998,"count":4,"deployment":{"_id":"554c92f979d26026307a5a9b","name":"Punjabi Bagh"}},{"taxAmount":304.9,"totalAmount":1111,"totalDiscount":0,"totalItems":9,"gTotal":1415.8999999999999,"count":2,"deployment":{"_id":"554c930579d26026307a5a9c","name":"Pitampura"}},{"deployment":{"_id":"554c930c79d26026307a5a9d","name":"Model Town"}},{"deployment":{"_id":"554c945079d26026307a5cae","name":"Dwarka"}},{"deployment":{"_id":"554cd66c0a40ccd2416e38e7","name":"Green Park"}}];
for( var prop in arrObjs ){
sumProps( arrObjs[i], objSums );
}
console.log( objSums );
}
function sumProps( objIn, newObjSum ){
for( var theProp in objIn ){
if( typeof objIn[ theProp ] === 'object'){
sumProps( objIn[ theProp ], newObjSum );
}else{
if( !newObjSum[ theProp ] ) newObjSum[ theProp ] = 0;
newObjSum[ theProp ] += objIn[ theProp ];
}
}
}
答案 6 :(得分:0)
您应该使用Array.prototype.reduce()方法。此外,您可以使用功能方式和高阶功能来实现。
此外,您可以使用Arguments对象来传递多个参数。
我已经制作了通用功能,以减少您的数据。
var data = [{"taxAmount":572.26,"totalAmount":2004,"totalDiscount":0,"totalItems":12,"gTotal":2576.2599999999998,"count":4,"deployment":{"_id":"554c92f979d26026307a5a9b","name":"Punjabi Bagh"}},
{"taxAmount":304.9,"totalAmount":1111,"totalDiscount":0,"totalItems":9,"gTotal":1415.8999999999999,"count":2,"deployment":{"_id":"554c930579d26026307a5a9c","name":"Pitampura"}},
{"deployment":{"_id":"554c930c79d26026307a5a9d","name":"Model Town"}},
{"deployment":{"_id":"554c945079d26026307a5cae","name":"Dwarka"}},
{"deployment":{"_id":"554cd66c0a40ccd2416e38e7","name":"Green Park"}}];
function reducer(data, func){
//Retrieve our arguments parameter
var args = [].slice.call(arguments);
//Remove first and second element from array args
args.splice(0,2);
//Start reducing our data, initialize with an empty object
return data.reduce(function(result, current){
//ForEach key
args.forEach(function(key){
//If our object get current key, add curent value, instead of initialize it with 0
//Apply our high order function
func(result, current, key);
});
//Return our current object result
return result;
}, {});
}
//Function to sum some properties
function sum(result, current, key){
return result[key] = (result[key] || 0) + (current[key] || 0);
}
//Function to count occurence of properties
function number(result, current, key){
return current.hasOwnProperty(key)
? (result[key] = (++result[key] || 1), result)
: result
}
var res1 = reducer(data, sum, 'taxAmount', 'totalAmount');
var res2 = reducer(data, number, 'taxAmount');
因此,只需指定高阶函数,您就可以使用一个函数对特定字段求和或计算出现次数。