I have an array in my Angular class containing number and object as shown below.
What I want to do is recreate the array with only number like so [5,8,9,1,7] i.e. I get the id of the object inside the array and append it to the array.
allowedDepartmentAccess = [
5,
8,
9,
{
"id": 1,
"name": "Human Resource",
"abbreviation": "HR"
},
{
"id": 7,
"name": "Warehouse",
"abbreviation": "WH"
}
]
2
Answers
In Angular, you can achieve this by using the map operator to transform each element of the array. If an element is a number, keep it as is; if it’s an object, extract its "id" property. Here’s an example:
You can use
Array.prototype.reduce()
to achieve this.