gpt4 book ai didi

javascript - 如何修复 angular.forEach 中对象的重复?

转载 作者:行者123 更新时间:2023-11-30 16:31:26 25 4
gpt4 key购买 nike

请帮助我,我该如何解决。

    schema.controller('schemaDetailCtrl', function($scope, ajaxCall, $location) {    $scope.storeDetails = [     {      "storeName": "Test Store",      "zipCode": "201301",      "idenNo": "999999"     },     {      "storeName": "Test2 Store",      "zipCode": "201014",      "idenNo": "200037"     },     {      "storeName": "Test3 Store",      "zipCode": "201014",      "idenNo": "200137"     }   ];    $scope.zipcodes = new Array();       angular.forEach($scope.storeDetails, function(store) {       this.push(store.zipCode);       console.log(store.zipCode);    }, $scope.zipcodes);    Expected Output :    201301    201014    201014    Actual Output:     Error: [ngRepeat:dupes] http://errors.angularjs.org/1.2.20/ngRepeat/dupes?p0=zipcode%20in%20zipcodes&p1=string%3A201014

最佳答案

此问题与您的 forEach 没有直接关系。它从 View 中抛出,可以通过将 track by 添加到 ng-repeat 语句来解决:

<div ng-repeat="zip in zipcodes track by $index">

如果您不希望数组中存在重复项,那么过滤掉它们将是一个单独的问题。

引用 ng-repeat docs

关于javascript - 如何修复 angular.forEach 中对象的重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33268422/

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