Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  21] [ 5]  / answers: 1 / hits: 17822  / 9 Years ago, thu, july 30, 2015, 12:00:00

My question goes like this. I have a JSON object below. 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.



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



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. Thanks


More From » angularjs

 Answers
9

None of this really needs Angular, because it involves pure JavaScript.



In each object, assignments is an array. 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. i.e. assignments[1] will become assignments[0].



If you don't want to change the indices, then delete is what you're looking for:



delete myObject[0].assignments[0];


This will however cause the first element of the array to have the value undefined.


[#65605] Wednesday, July 29, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmieo

Total Points: 515
Total Questions: 102
Total Answers: 110

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;