How to transform an array recursively?

Asked

Viewed 116 times

3

I have the following array:

let exemplo = [{
  alarm: {
    title: "Pai",
    id: "1"
  },
  children: [],
  parent: "",
}, {
  alarm: {
    title: "Filho",
    id: "2",
  },
  parent: "Pai",
  children: [],
}, {
  alarm: {
    title: "Neto",
    id: "3",
  },
  parent: "Pai",
  children: [],
}, {
  alarm: {
    title: "Filho 2",
    id: "4",
  },
  parent: "Pai",
  children: [],
}, {
  alarm: {
    title: "Neto 2",
    id: "5",
  },
  parent: "Filho",
  children: [],
}, {
  alarm: {
    title: "Pai 2",
    id: "6",
  },
  parent: "",
  children: [],
}, {
  alarm: {
    title: "Filho 2.2",
    id: "6",
  },
  parent: "Pai 2",
  children: [],
}];

How can I turn him recursively in an array of objects based on the condition that a larm must be the son of a Alarm Parent?

I tried to use the function below but was unsuccessful:

public getNestedChildren(arr, parent) {
    var out = [];

    for (var i in arr) {
      if (arr[i].parent === parent) {
        console.log(arr[i].alarm.parent);
        var children = getNestedChildren(arr, arr[i]);

        if (children.length) {
          arr[i].children = children;
        }
        out.push(arr[i]);
      }
    }

    return out;
}

var nest = this.getNestedChildren(exemplo, '');

The expected exit would be something like:

let exemplo = [
  {
    alarm: {
      title: "Pai",
      id: "1"
    },
    parent: "",
    children: [
      {
        alarm: {
          title: "Filho",
          id: "2",
        },
        parent: "Pai",
        children: [
          {
            alarm: {
              title: "Neto",
              id: "3",
            },
            parent: "Filho",
            children: [],
          }
        ]
      }
    ]
  }

Always obeying this hierarchy, father, son, grandson, etc., regardless of the amount of alarms.

1 answer

5


Does it have to be recursive? I did not see the need for this in this example, if to undo the formatting would make sense, but there is no reason to use recursiveness in a shallow list.

I find it easier for you to create an associative array (an object) using the alarm name as a key, so you don’t have to go through the entire array when it comes to finding the parent.

Note: all Javascript arrays are associative, even the enumerable ones, so this does not affect the unemployment.

function transformar(alarmes) {
  const listaClone    = JSON.parse(JSON.stringify(alarmes))
  const listaIndexada = {}
  const listaRetorno  = []

  for (const alarme of listaClone)
    listaIndexada[alarme.alarm.title] = alarme

  for (const alarme in listaIndexada) if (!listaIndexada[alarme].parent)
    listaRetorno.push(listaIndexada[alarme])
  else
    listaIndexada[listaIndexada[alarme].parent].children.push(listaIndexada[alarme])
	
  return listaRetorno
}

const exemplo = [{"alarm":{"title":"Pai","id":"1"},"children":[],"parent":""},{"alarm":{"title":"Filho","id":"2"},"parent":"Pai","children":[]},{"alarm":{"title":"Neto","id":"3"},"parent":"Pai","children":[]},{"alarm":{"title":"Filho 2","id":"4"},"parent":"Pai","children":[]},{"alarm":{"title":"Neto 2","id":"5"},"parent":"Filho","children":[]},{"alarm":{"title":"Pai 2","id":"6"},"parent":"","children":[]},{"alarm":{"title":"Filho 2.2","id":"6"},"parent":"Pai 2","children":[]}]
const listaTransformada = transformar(exemplo)

console.log(listaTransformada)

Browser other questions tagged

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