Sunday, November 4, 2018

Adding and removing an attribute from the entire group of array: ES6 Javascript

Adding and removing an attribute from the object array

I mostly like to elaborate things that I am writing about but sometimes answers need to be kept short and crisp, so I will get down to the answers right away.

1. Adding an attribute:

Entity:

export class Case {

  caseId: string;
  name: string;
  description: string;
}

Method to add attribute:

I need to add checked attributed to the array of Case:
const checkedCases = cases.map(obj => {
  return ({...obj, checked : false});
});

2. Removing the attribute

Entity:

export class CheckedCase {
  caseId: string;
  name: string;
  description: string;
  checked: boolean;
}

Method to remove attribute:

I need to add checked attributed to the array of Case:
const cases = checkedCases.map(obj => {
  delete obj.checked;
  return obj;
});

No comments:

Post a Comment