I have the array of Objects and want to concat only the list property of all objects and create a new array. So I have a "combinedList" getter in the below code to combine the list if array length is greater than 1 else just return the 1st items list property. This returned array will be used in the template file to display. If any new item is added, the addComment function will be called with the newly added item and push that to the "combinedList" array.
The problem here is, if the objArr length is less than 1, the newItem is pushed to the "combineList". But, if it is greater than 1, newItem is not pushed to the "combinedList" array.
Can anyone please help me to figure out the cause for this issue?
export default class YourComponent extends Component {
objArr = [ {id:1, list:[1,2,3]}, {id:2, list:[3,4]} ];
get combinedList() {
if (this.objArr.length > 1) {
let list = [];
this.objArr.forEach((item) => {
list.push(...item.list);
});
return list;
}
return this.objArr[0].list;
}
// Call this method when adding a new item
addComment(newItem) {
this.combinedList.unshiftObject(newItem);
}
}
Aucun commentaire:
Enregistrer un commentaire