gpt4 book ai didi

javascript - 子文档组未获取正确的值

转载 作者:行者123 更新时间:2023-11-27 23:22:55 27 4
gpt4 key购买 nike

我正在使用 AngularJS 对数组的所有子文档进行分组。它只能对子文档的第一项进行分组,并给出长度,我如何获得其正确长度的计数。我的plunk link .

我现在得到的结果是

Isnain Meals - 1

Chicken Burger - 2

预期结果

Isnain Meals - 2

Chicken Burger - 2

HTML

<body ng-controller="MainCtrl">
<div ng-repeat="(key, data) in groupedByFoodName">
<p>{{key}} - {{data.length}}</p>
</div>
</body>

Controller JS

$scope.groupedByFoodName=  _.groupBy($scope.lists, function (each) { return each.orderfood[0].name });

JSON

  $scope.lists = [
{
"_id": "56b0c315e179bb0e00a44dbf",
"orderfood": [
{
"_id": "569d865bff1fe20e00f8ba97",
"qty": "1",
"confirm": true,
"price": 154,
"name": "Isnain Meals"
},
{
"_id": "569d865bff1fe20e00f8ba98",
"qty": "1",
"confirm": true,
"price": 154,
"name": "Isnain Meals"
}
],
"content": "9176649143",
"created": "2016-02-02T14:54:13.926Z"
},
{
"_id": "56b06ed25b53250e00ccbd73",
"orderfood": [
{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": true,
"price": 125,
"name": "Chicken Burger"
}
],
"content": "6886058585",
"created": "2016-02-02T08:54:42.986Z"
},
{
"_id": "56b06ed25b53250e00ccbd74",
"orderfood": [
{
"_id": "569d84f04834c10e003dff37",
"qty": "1",
"confirm": true,
"price": 125,
"name": "Chicken Burger"
}
],
"content": "6886058585",
"created": "2016-02-02T08:54:42.986Z"
}];

最佳答案

试试这个

var groupedByFoodName=  _.chain(lists).map(function(each){ return each.orderfood}).flatten().groupBy(function (each) { console.log(each); return each.name  }).value();

你写的不太正确_.groupBy,它只返回数组orderfood的第一个名称。

jsfiddle 上的实例.

<form name="ExampleForm" id="ExampleForm">
<div ng-repeat="(key, data) in groupedByFoodName">
<p>{{key}} - {{data.length}}</p>
</div>
</form>

还有 JS:

    $scope.lists = [{
"_id": "56b0c315e179bb0e00a44dbf",
"orderfood": [{
"_id": "569d865bff1fe20e00f8ba97",
"qty": "1",
"confirm": true,
"price": 154,
"name": "Isnain Meals"
}, {
"_id": "569d865bff1fe20e00f8ba98",
"qty": "1",
"confirm": true,
"price": 154,
"name": "Isnain Meals"
}],
"content": "9176649143",
"created": "2016-02-02T14:54:13.926Z"
}, {
"_id": "56b06ed25b53250e00ccbd73",
"orderfood": [{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": true,
"price": 125,
"name": "Chicken Burger"
}],
"content": "6886058585",
"created": "2016-02-02T08:54:42.986Z"
}, {
"_id": "56b06ed25b53250e00ccbd74",
"orderfood": [{
"_id": "569d84f04834c10e003dff37",
"qty": "1",
"confirm": true,
"price": 125,
"name": "Chicken Burger"
}],
"content": "6886058585",
"created": "2016-02-02T08:54:42.986Z"
}];

$scope.groupedByFoodName = _.chain($scope.lists).map(function(each) {
return each.orderfood
}).flatten().groupBy(function(each) {
return each.name
}).value();

关于javascript - 子文档组未获取正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35220121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com