本文介绍了如何不推所有阵列没有这个产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有此代码,我使用了以下json:

for example i have this code i used this json:

"food": {
        "appetizers": [
            {
                "id": 1,
                "image": "../image/calammari.png",
                "title": "rings",
                "price": 11500,
                "ingredient":[{
                    "id" : "0001",
                    "name": "avocado"
                },
                {
                    "id" : "0001",
                    "name": "tomato"
                }
            ]
            },
            {
                "id": 2,
                "image": "../image/food2.png",
                "title": "bang bang",
                "price": 10000,
                "ingredient":[{
                    "id" : "0001",
                    "name": "eggplant"
                },
                {
                    "id" : "0001",
                    "name": "cucumber"
                }
            ]
            }

从这个json文件中,如果数组我的访问权限等于Tomato,我只想显示含番茄的食物.

from this json file if the array my access equal tomato , just i want to display the food that have tomato.

所以我用了这个html:

so i used this html:

  <div ng-repeat="appetizer in appetizers ">
                        <div>
                            <img ng-src="{{appetizer.image}}" />
                           <div >
                                <p >
                                    {{appetizer.title | uppercase}}
                                </p>
                            </div>

和此javascript:

and this javascript :

  var myAccess = ["tomato"];

      $scope.test = [];
      var appetizer = $scope.appetizers;
      for (var i = 0; i < $scope.appetizers.length; i++) {
        for (var j = 0; j < $scope.appetizers[i].ingredient.length; j++) {

            if ($scope.appetizers[i].ingredient[j].name === myAccess) {

// what should i write here

            }
          }
      }
      return null;    }

很抱歉,如果有人可以帮忙的话!!

sorry its about this if someone can help please !!

所以myAccess =番茄,应该先阅读一下番茄配料中的开胃菜,而我只想推送含有番茄的配料.

so myAccess = tomato , and should read the first of the appetizer that in the ingredient tomato , and i want to push just the ingredient that have tomato .

推荐答案

这可能对您有帮助

var app=angular.module("myapp",[]);
app.controller("test_ctrl",function($scope){
$scope.appetizers = [];
$scope.temp={
	"food": {
		"appetizers": [
				{
					"id": 1,
					"image": "../image/calammari.png",
					"title": "rings",
					"price": 11500,
					"ingredient":[
						{
							"id" : "0001",
							"name": "avocado"
						},
						{
							"id" : "0001",
							"name": "tomato"
						}
					]
				},
				{
					"id": 2,
					"image": "../image/food2.png",
					"title": "bang bang",
					"price": 10000,
					"ingredient":[
						{
							"id" : "0001",
							"name": "eggplant"
						},
						{
							"id" : "0001",
							"name": "cucumber"
						}
					]
				}
			]
		}
}
            
            
            var myAccess = ["tomato"];

      
      var appetizer = $scope.temp.food.appetizers;
      for (var i = 0; i < appetizer.length; i++) {
        for (var j = 0; j < appetizer[i].ingredient.length; j++) {
            if (appetizer[i].ingredient[j].name === myAccess[0]) {
                $scope.appetizers.push(appetizer[i]);

            }
          }
      }
         
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>


<div ng-app="myapp" ng-controller="test_ctrl">
<div ng-repeat="appetizer in appetizers ">
    <div>
        <img ng-src="{{appetizer.image}}" />
       <div >
            <p >
                {{appetizer.title | uppercase}}
            </p>
        </div>
    </div>
							
</div>

这篇关于如何不推所有阵列没有这个产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 18:52