简体   繁体   English

如何在 Angular 中删除 JSON 对象内的对象?

[英]How do I remove an object inside a JSON object in Angular?

My question goes like this.我的问题是这样的。 I have a JSON object below.我在下面有一个 JSON 对象。 I want to remove the object that has an assignment 1. I already looped through it and by the best of me I cannot seem to remove that particular object.我想删除分配为 1 的对象。我已经遍历了它,我最好的人似乎无法删除该特定对象。

0: Object
  teller_id: 1
  details: CASH
  assignments: Array [2]
   0: Object  <---- Remove this Object and all the elements indside it
      service_id: 1
      status: 1
      assignment: 1
   1: Object
      service_id: 1
      status: 1
      assignment: 2
1: Object
 teller_id: 2
 details: EMP
 assignments: Array [2]
   0: Object
      service_id: 2
      status: 3
      assignment: 4
   1: Object
      service_id: 2
      status: 4
      assignment: 6

Remove object with an assignment of 1删除赋值为 1 的对象

0: Object
  teller_id: 1
  details: CASH
  assignments: Array [2]
   1: Object
      service_id: 1
      status: 1
      assignment: 2
1: Object
 teller_id: 2
 details: EMP
 assignments: Array [2]
   0: Object
      service_id: 2
      status: 3
      assignment: 4
   1: Object
      service_id: 2
      status: 4
      assignment: 6

In which it removes the Object[0] found inside the assignments array.它删除了在 assignments 数组中找到的 Object[0]。 Thanks谢谢

None of this really needs Angular, because it involves pure JavaScript.这些都不需要 Angular,因为它涉及纯 JavaScript。

In each object, assignments is an array.在每个对象中, assignments都是一个数组。 Whilst an array is a form of object, it has its own properties.虽然数组是对象的一种形式,但它有自己的属性。 There are a few ways to approach this.有几种方法可以解决这个问题。

Firstly if you wish to treat it as an array, then:首先,如果您希望将其视为数组,则:

myObject[0].assignments.splice(0,1); // remove first element from array

or:或者:

myObject[0].assignments.shift(); // get first element from array

This will however move the indexes of the assignments down in the array.然而,这将在数组中向下移动分配的索引。 ie assignments[1] will become assignments[0].即 assignments[1] 将变成 assignments[0]。

If you don't want to change the indices, then delete is what you're looking for:如果你不想改变指数,然后delete是你在找什么:

delete myObject[0].assignments[0];

This will however cause the first element of the array to have the value undefined .然而,这将导致数组的第一个元素具有值undefined

Let the variable name that points to the object is ar :让指向对象的变量名是ar

delete ar[0].assignments[0];

I hope you don't have trouble looping and finding the element.我希望您在循环和查找元素时不会遇到问题。

You can use splice function for delete elements from array您可以使用 splice 函数从数组中删除元素

var arr = [{name: 'Ram', assiignment: 1}, {name: 'Raju', assiignment: 2}];
arr.splice(0,1); // Here 0 is the index of element to be delete and 1 is how many elements to delete from that index we provided

After your array will like this:之后你的阵列会像这样:

[{name: 'Raju', assiignment: 2}]

you can do something like你可以做类似的事情

angular.forEach(items, function (item) {
    for(var i = item.assignments.length - 1; i >=0; i--) {
        if (item.assignments[i].assignment === 1) {
            item.assignments.splice(i, 1);
        }
    } 
});

This is problematic with angular native.这对于 angular native 是有问题的。

If you are using some third party library like lodash or underscore, it will be easier.如果你使用的是像 lodash 或 underscore 这样的第三方库,它会更容易。

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

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