简体   繁体   English

如果属性不存在,则不显示对象的数据

[英]Don't display object's data if property doesn't exist

I want to choose not to display an object's data if a property is missing from the object. 如果对象缺少属性,我想选择不显示对象的数据。 I'm calling data from a REST API that removes a price_1 property from an object if the item is no longer available for purchase. 我正在从REST API调用数据,如果该商品不再可供购买,则该REST API会从对象中删除price_1属性。 I've done a little bit of research into delete but from my understanding that's only for deleting a reference to a property, not an object. 我已经对delete进行了一些研究,但据我了解,这仅用于删除对属性而不是对象的引用。 I'm not sure if it's possible to entirely remove an object if a property doesn't exist, but this would be the ideal solution. 我不确定如果属性不存在是否可以完全删除对象,但这将是理想的解决方案。 I also want to know if the solution would be better suited as a custom filter. 我还想知道该解决方案是否更适合作为自定义过滤器。

I've tried the following: 我尝试了以下方法:

angular.module('foldsApp.productModule', [])
    .controller('ProductsController', ['$scope', '$log', 'APIService', function ($scope, $log, APIService) {
        $scope.products = {};

        APIService.getData()
            .success(function(res) {
                $scope.products = res.results;
                for (price in $scope.products) {
                    if($scope.products[price].price_1 === 'undefined') {
                        // remove object?
                    }
                }
            })
            .error(function(data, status, headers, config) {
                $log.log('error: ' + data);
            });

    }]);

Right now, the if($scope.products[price].price_1 === 'undefined') equates to false . 现在, if($scope.products[price].price_1 === 'undefined')等于false I'm not entirely sure why this is happening, but I assume it's because price_1 does exist on most objects in $scope.products and I'm not sure how to work with that. 我不完全确定为什么会这样,但是我想是因为price_1$scope.products大多数对象上确实存在,而且我不确定该如何使用。 This is my first foray into "higher" level JavaScript, so a few things are going over my head. 这是我对“高级” JavaScript的首次尝试,因此有些事情困扰着我。 Any helpful resources are greatly appreciated, thanks. 非常感谢任何有用的资源,谢谢。

HTML if that matters: HTML,如果这很重要:

<ul>
    <li dir-paginate="product in products | itemsPerPage: 30">
        <a href="{{ product.link_1 }}"><img src="{{ product.category_product_image_placeholder_image }}" /></a>
        <br>
        {{ product.product_name_text }}
        <br>
        <strong>{{ product.price_1 | currency }}</strong>
    </li>
</ul>

to check for undefined specifically you should use typeof variable === 'undefined' : 特别要检查未定义,您应该使用typeof variable === 'undefined'

so: 所以:

if(typeof $scope.products[price].price_1 === 'undefined'){
    delete $scope.products[price];
}

delete will remove properties of an object (which is what it looks like you have) and if they are not referenced anywhere else the garbage collector will pick them up. delete会删除对象的属性(看起来像您的属性),如果在其他任何地方都未引用它们,则垃圾收集器会选择它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM