I am currently using Angular 6 and dealing with arrays. I have an array along with a model.
Here is the Array:
let array = [
{
id: 1,
value: "Some Value",
description: "Some Description"
},
{
id: 2,
value: "Some Value",
description: "Some Description"
},
{
id: 3,
value: "Some Value",
description: "Some Description"
},
]
Now, let's take a look at the Model:
export class Data{
index: number;
id: number;
value: string;
description: string;
constructor(data){
this.index = null;
this.id = data.id;
this.value = data.value;
this.description = data.description;
}
}
When selecting an item from the array, a new object is created using this method.
selectItem(index, data){
let selectedItem = new Data(data);
console.log(selectedItem);
}
The expected result is:
{
id: 1,
value: "Some Value",
description: "Some Description"
}
In this scenario, it becomes challenging to include the index value in the newly created object as the data object lacks this parameter.
To address this issue, I implement the following approach:
selectItem(index, data){
let selectedItem = new Data({
index : index,
id : data.id,
value : data.value,
description : data.description
})
}
Now onto my query: Is there a way to add additional parameters while creating a new object utilizing models? A similar concept is illustrated below:
selectItem(index, data){
let selectedItem = new Data({data}).index = index
}