How to parse from one object list to another?

Asked

Viewed 86 times

0

I have a list of tasks stored in an object items, I want to parse this list so that, anything with status open is stored in the variable openTasks and everything with status completed is storing in completedTasks.

I even scribbled something but it didn’t work out. Look:

export class AllTasksPage {

  openTasks;
  completedTasks;

  items = [
    {'title': 'Entregar relatório', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Fazer compras', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Concluir trabalho da faculdade', 'note': '', 'image': '', 'status': 'concluida'},
    {'title': 'Ligar para cliente', 'note': '', 'image': '', 'status': 'concluida'},
    {'title': 'Revisar trabalho', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Falar com chefe', 'note': '', 'image': '', 'status': 'concluida'},
    {'title': 'Estudar apra prova', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Buscar roupa na lavanderia', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Limpar a casa', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Estudar para apresentação', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Assistir filme xpto', 'note': '', 'image': '', 'status': 'aberta'},
    {'title': 'Colocar a roupa para lavar', 'note': '', 'image': '', 'status': 'aberta'}
  ];

  constructor(public navCtrl: NavController, public alertCtrl: AlertController) {

    for (let item of this.items) {
      if (item.status == 'aberta') {
        this.openTasks.concat(item);
      }
    }
  }

In short: I want openTasks and comletedTasks have the same existing structure in items, however, the data will change based on the status of the tasks.

  • You can use the filter to pass data from one array to another, the first will only be with the open and the other with the completed

1 answer

1


You can use the Array.prototype.filter() to filter items from an array.

const items = [
  {'title': 'Entregar relatório', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Fazer compras', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Concluir trabalho da faculdade', 'note': '', 'image': '', 'status': 'concluida'},
  {'title': 'Ligar para cliente', 'note': '', 'image': '', 'status': 'concluida'},
  {'title': 'Revisar trabalho', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Falar com chefe', 'note': '', 'image': '', 'status': 'concluida'},
  {'title': 'Estudar apra prova', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Buscar roupa na lavanderia', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Limpar a casa', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Estudar para apresentação', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Assistir filme xpto', 'note': '', 'image': '', 'status': 'aberta'},
  {'title': 'Colocar a roupa para lavar', 'note': '', 'image': '', 'status': 'aberta'}
];

const filterByStatus = (status) => {
  return items.filter((item) => item.status === status);
};

const onlyOpened = filterByStatus("aberta");
const onlyCompleted = filterByStatus("concluida");

console.log("Only opened", onlyOpened);
console.log("Only completed", onlyCompleted);

The method has no side effects, and returns a new filtered array.

  • Another possibility is when checking the status and it is not "opened" add in onlyCompleted, if it is "completed" just returns true so it is not necessary to go through the same array twice in a row

Browser other questions tagged

You are not signed in. Login or sign up in order to post.