How to modify an object array through a condition?

Asked

Viewed 75 times

1

I have the following object array:

let items = [
{
      carro: {
        title: "Gol",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true,
},
{
      carro: {
        title: "Gol Filho",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true,
      parent: 'Gol'
}]

There is a simple way to loop inside this array and check if any element has the string Parent equal to title of some element and add that element within the object that satisfies that condition?

Example:

let items = [
{
      carro: {
        title: "Gol",
        description: "Gol quadrado",
        active: true,
        },
      isVisible: true,
      children: [
          {
          carro: {
            title: "Gol Filho",
            description: "Gol quadrado",
            active: true,
            },
            isVisible: true,
            parent: 'Gol'
          }
      ]
}]
  • The logic is this, take everyone who doesn’t have parent and take everyone who has parent and make the junction of information, there is nothing magical needs to be done so some code to separate and then merge, you made some code?

  • You changed the original question and there is no recursion of children with children’s children, nor does the question make this kind of junction so the answer does what you ask and I will reverse the edition, if you want to please open another question and be as clear as possible.

2 answers

3


And something like that, I think it solves your problem, where it was split in two array of objects the items you have parent and not and then made the junction with filter, example:

// matriz 
let items = [{
    carro: {
      title: "Gol",
      description: "Gol quadrado",
      active: true,
    },
    isVisible: true,
  },
  {
    carro: {
      title: "Fusca",
      description: "Fusca 1.6",
      active: true,
    },
    isVisible: true    
  },
  {
    carro: {
      title: "Gol Filho",
      description: "Gol quadrado",
      active: true,
    },
    isVisible: true,
    parent: 'Gol'
  },
  {
    carro: {
      title: "Gol Filho 2",
      description: "Gol Bolinha",
      active: true,
    },
    isVisible: true,
    parent: 'Gol'
  }
];

//criando duas variáveis para separação
let newItems = [];
let newParents = [];

//map separando os itens, poderia ser um simples for
items.map(x => {
  if (Object.getOwnPropertyDescriptor(x, 'parent')) {
    newParents.push(x);
  } else {
    newItems.push(x);
  }
});

//criando as junções se for possivel
newItems.map(x => {
  x['children'] = newParents.filter(a => a.parent == x.carro.title);
});

//mostrando os itens final
console.log(newItems);


A well summarized form:

// matriz 
let items = [{
    carro: {
      title: "Gol",
      description: "Gol quadrado",
      active: true,
    },
    isVisible: true,
  },
  {
    carro: {
      title: "Fusca",
      description: "Fusca 1.6",
      active: true,
    },
    isVisible: true    
  },
  {
    carro: {
      title: "Gol Filho",
      description: "Gol quadrado",
      active: true,
    },
    isVisible: true,
    parent: 'Gol'
  },
  {
    carro: {
      title: "Gol Filho 2",
      description: "Gol Bolinha",
      active: true,
    },
    isVisible: true,
    parent: 'Gol'
  }
];

const sorted = items.filter(x => {
    if (!Object.getOwnPropertyDescriptor(x, 'parent')) {
      x['children'] = items.filter(a => a.parent == x.carro.title);
      return x;
    }
});

console.log(sorted);

  • 1

    It’s perfect, just a question, if I always needed to add an extra child if an element’s Parent is a child, how could I? Something recursive that I say.

  • I didn’t understand your question @haykou the code demonstrated I think it already does that! I even made a minor also take a look!

  • I thank Virgilio, it is that I am trying to add a Children inside another using the Parent and it does not recognize

1

This way supports recursion:

const childrens = {};

const parentNames = items.filter(item => item.parent).map(item => {
  if(!childrens[item.parent]) {
    childrens[item.parent] = [];
  }
  childrens[item.parent].push(item);
  return item.parent;
});

const itemsWithChildrens = items.map(item => {
  if(parentNames.indexOf(item.carro.title) !== -1) {
    item.children = childrens[item.carro.title];
  }
  return !item.parent ? item : false;
}).filter(item => item);

I tested with the following set:

let items = [
{
      carro: {
        title: "Gol",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true,
},
{
      carro: {
        title: "Gol Filho",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true,
      parent: 'Gol'
},
{
      carro: {
        title: "Outro carro",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true
},
{
      carro: {
        title: "Filho do outro carro",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true,
      parent: 'Outro carro'
},
{
      carro: {
        title: "Segundo neto",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true,
      parent: 'Gol Filho'
},
{
      carro: {
        title: "Gol Neto",
        description: "Gol quadrado",
        active: true,
      },
      isVisible: true,
      parent: 'Gol Filho'
}]

Browser other questions tagged

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