创建一个抓取每个数组项并对其进行整理的JavaScript对象

时间:2016-02-02 12:55:39

标签: javascript angularjs

我正在尝试创建一个JavaScript对象来抓取 BusinessData 中的每个services数组,然后按如下方式对其进行整理:

services: [
                {
                    id: 1,
                    businessId: 1,
                    title: 'Brake Fluid Refresh'
                },
                {
                    id: 2,
                    businessId: 1,
                    title: 'Diagnostics'
                },
                {
                    id: 3,
                    businessId: 1,
                    title: 'Coolant Refresh'
                },
                {
                    id: 4,
                    businessId: 1
                    title: 'Oil Change'
                },
                {
                    id: 5,
                    businessId: 1,
                    title: 'MOT'
                },
                {
                    id: 6,
                    businessId: 1,
                    title: 'Summer Check'               
                },
                {
                    id: 7,
                    businessId: 1,
                    title: 'Winter Check'                   
                }           
            ]

到目前为止,这是我的代码:

var businessData = [
        {
            id: 1,
            categoryId: 1,
            name: 'Japan Center Garage',
            services: [
                {
                    id: 1,
                    businessId: 1,
                    title: 'Brake Fluid Refresh'
                },
                {
                    id: 2,
                    businessId: 1,
                    title: 'Diagnostics'
                }               
            ]
        },
        {
            id: 2,
            categoryId: 1,
            name: 'Rex Garage',
            services: [
                {
                    id: 3,
                    businessId: 1,
                    title: 'Coolant Refresh'
                },
                {
                    id: 4,
                    businessId: 1
                    title: 'Oil Change'
                }   
            ]
        },
        {
            id: 3,
            categoryId: 1,
            name: 'Mission & Bartlett Garage',
            services: [
                {
                    id: 5,
                    businessId: 1,
                    title: 'MOT'
                },
                {
                    id: 6,
                    businessId: 1,
                    title: 'Summer Check'               
                },
                {
                    id: 7,
                    businessId: 1,
                    title: 'Winter Check'                   
                }               
            ]
        }               
    ]

    return {
        getAllCategories: function () {
            return categoryData;
        },

        getAllServices: function () {
            var servicesData = {
                services: []
            };
            for(var i = 0; i < businessData.length; i++) {
                if(businessData[i].services) {
                    servicesData['services'].push(businessData[i].services)
                }
            }
            return servicesData;
        },

        getAllBusinesses: function () {
            return businessData;
        },
    }
}])

它不能很好地工作,并且在从我的控制器调用它之后产生了一个我在视图中无法访问的数组:

<div class="item item-divider">
Service
</div>
<ion-list>
<ion-item ng-repeat="item in serviceList | orderBy:'title'">
  <p>{{item.title}}</p>
</ion-item>
</ion-list>
<div ng-if="item !== undefined && !item.length" class="no-results">
<div class="item" style="border-top: 0px">
    <p>No Results</p>
</div>
</div>

UPDATE:从我的控制器调用serviceList:

$scope.serviceList = ServicesData.getAllServices();

有人可以告诉我出了什么问题吗?

2 个答案:

答案 0 :(得分:2)

var services = [];

businessData.forEach(function (business) {
    Array.prototype.push.apply(services, business.services);
});

你几乎就在那里,但是你正在将一系列服务推向一个数组。通过使用Array.prototype.push.apply,我们将服务推送到数组。这最终会给你你想要的结果:)

答案 1 :(得分:1)

您可以使用lodash,最好将其添加到工具箱中。

以下是lodash的外观:

_.flatMap(businessObject, (d) => d.services)

甚至

_.flatMap(businessObject, "services")

为了使它更加冗长,你正在制作地图,然后展平:

var mapped = _.map(businessObject, "service")
var services = _.flatten(mapped)