如何使用ng-repeat添加或修改数组中的现有对象 [英] How to add or modify an existing object inside an array using ng-repeat

查看:64
本文介绍了如何使用ng-repeat添加或修改数组中的现有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组。每个对象都有三个属性,id,name和quantity。



我写了一个函数将对象推送到数组。



但是我希望函数必须检查对象是否已存在于数组中。如果确实存在,则该函数必须仅增加quantity属性。否则它必须添加一个新对象。



我尝试过:



i不知道怎么做......

i have an array of objects. Each object has three properties, id,name and quantity.

I wrote a function to push an object to the array.

but i want the function must check if the object already exists within the array. If it does exist the function must increment the quantity property only. Otherwise it must add a new object.

What I have tried:

i don't know how to do it......

推荐答案

试试这个,

根据你的需要定制它。



try this,
customise it as per your need.

<html>
<head>


    <!--<script src="angular.min.js"></script>-->
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

    <script>
        var app = angular.module('myapp', []);
        app.controller('myctrl', myctrl);

        function myctrl(


scope){


scope.myarray = [{id: 1 ,name:' apple',数量: 0 },
{id: 2 , name:' orange',数量: 0 },
{id: 3 ,name:' grape',数量: 0 }];
scope.myarray = [{ id: 1, name: 'apple', qty: 0 }, { id: 2, name: 'orange', qty: 0 }, { id: 3, name: 'grapes', qty: 0 }];


这篇关于如何使用ng-repeat添加或修改数组中的现有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆