首先,我有一个带有如下对象元素的数组:
$scope.selectedIngredient = [object1, object2, object3, object1, object2];
其次,我有一个新对象:
$scope.selectedIngredientResults = {};
现在我想在这样的javascript中实现一个功能:
计算数组的每个对象元素并将它们放入一个新对象并存储每个
计数
$scope.selectedIngredientResults = [
{"object" : object1, "count" : 2},
{"object" : object2, "count" : 2},
{"object" : object3, "count" : 1}
];
[![这是新对象的结果,这就是我想要的,
我正在尝试使用
$scope.addIngredient = function(selectedElement) {
console.log('selectedElement', selectedElement);
$scope.selectedIngredient.push(selectedElement);
console.log('$scope.selectedIngredient' , $scope.selectedIngredient);
$scope.selectedIngredientResults = {};
var lengthofR = $scope.selectedIngredient.length;
for (var i = lengthofR - 1; i >= 0; i--) {
var selected = $scope.selectedIngredient[i]['ingredient_id'];
console.log('lengthofR', lengthofR);
if(selected){
if($scope.selectedIngredientResults.hasOwnProperty(selected)){
$scope.selectedIngredientResults[$scope.selectedIngredient]++;
}else {
$scope.selectedIngredientResults[$scope.selectedIngredient] = 1;
}
console.log('$scope.selectedIngredientResults', $scope.selectedIngredientResults );
}
}
}
答案 0 :(得分:1)
首先,您需要通过密钥应用分组。然后从分组中提取结果。这是你需要的变换函数。
function transform(items) {
items = items.reduce(function (memo, val) {
var key = val['ingredient_id'];
if (!memo[key]) {
memo[key] = [];
}
memo[key].push(val);
return memo;
}, {});
var result = [];
for (var k in items) {
var groups = items[k];
result.push({ 'object': groups[0], 'count': groups.length });
}
return result;
}
答案 1 :(得分:0)
function getIngredientResults (ingredients) {
var source = [].slice.call(ingredients)
var result = []
while (source.length > 0) {
// Move first item from source.
var current = source.shift()
// Find indexes of duplicate entries.
var duplicateIndexes = source.reduce(function (carry, item, index) {
if (item.ingredient_id === current.ingredient_id) {
carry.push(index)
}
return carry
}, [])
// Remove duplicates from source.
duplicateIndexes.forEach(function (index) {
source.splice(index, 1)
})
// Add data to result
result.push({
ingredient: current,
count: duplicateIndexes.length + 1
})
}
return result
}
答案 2 :(得分:0)
我想这已经得到了回答,但是现在如何在一个减少中做到这一点:
$scope.selectedIngredientResults = $scope.selectedIngredient.reduce(function (result, item) {
var foundIndex = -1;
var found = result.some(function (alreadyCounted, i) {
if (alreadyCounted.object.ingredient_id === item.ingredient_id) {
foundIndex = i;
return true;
}
return false;
});
if (found && foundIndex !== -1) result[foundIndex].count += 1;
else result.push({object: item, count: 1});
return result;
}, []);
答案 3 :(得分:0)
**我花了一些时间来学习这些新方法,让你们回答并从这些方法中获得灵感并以另一种方式解决问题,非常感谢你们**
myApp.controller('addIngredientCtrl', function($scope) {
$scope.selectedIngredient = [];
$scope.addIngredient = function(selectedElement) {
var elementIndex = $scope.selectedIngredient.indexOf(selectedElement);
if(elementIndex === -1) {
selectedElement['customerCartQuantity'] = 1;
$scope.selectedIngredient.push(selectedElement);
} else {
$scope.changeIngredientQuantity($scope.selectedIngredient[elementIndex], 1);
}
}
$scope.changeIngredientQuantity = function(selectedElement, value) {
var elementIndex = $scope.selectedIngredient.indexOf(selectedElement);
$scope.selectedIngredient[elementIndex].customerCartQuantity += value;
if($scope.selectedIngredient[elementIndex].customerCartQuantity == 0) {
$scope.selectedIngredient.splice(elementIndex, 1);
}
} });