1

I have a problem transforming an array of objects to another array of nested objects. How can I transform table to look like transformedTable in the example code below?

Input data:

const table = [
  {id: 1, isMain: null, parentId: null, name:"john"},
  {id: 2, isMain: true, parentId: null, name:"sam"},
  {id: 3, isMain: null, parentId: 2, name:"samantha"},
  {id: 4, isMain: true, parentId: null, name:"kate"},
  {id: 5, isMain: true, parentId: 4, name:"jonathan"},
  {id: 6, isMain: null, parentId: 4, name:"walter"},
  {id: 7, isMain: null, parentId: 5, name:"clara"}
]

I want to transform the data above to something like this:

transformedTable = [{
    id: 1,
    isMain: null,
    parentId: null,
    name: "john"
  },
  {
    id: 2,
    isMain: true,
    parentId: null,
    name: "sam",
    kids: [{
      id: 3,
      isMain: null,
      parentId: 2,
      name: "samantha"
    }]
  },
  {
    id: 4,
    isMain: true,
    parentId: null,
    name: "kate",
    kids: [{
        id: 5,
        isMain: true,
        parentId: 4,
        name: "jonathan",
        kids: [{
          id: 7,
          isMain: null,
          parentId: 5,
          name: "clara"
        }]
      },
      {
        id: 6,
        isMain: null,
        parentId: 4,
        name: "walter"
      },
    ]
  },
]
4
  • Just iterate through the array and form the new one. Commented Oct 5, 2018 at 12:33
  • i know it but how/?? position on object can be shuffled Commented Oct 5, 2018 at 12:36
  • Then sort it first? Commented Oct 5, 2018 at 12:42
  • Any updates on this? There is an answer below. Commented Oct 8, 2018 at 15:59

1 Answer 1

2

You could nest a couple of loops to compare each object and add the "kids" property where needed. Then, filter the resulting array to leave just the ultimate parents (which contain all the nested children). See working snippet below:

const table = [{ id: 1, isMain: null, parentId: null, name:"john" }, { id: 2, isMain: true, parentId: null, name:"sam" }, { id: 3, isMain: null, parentId: 2, name:"samantha" }, { id: 4, isMain: true, parentId: null, name:"kate" }, { id: 5, isMain: true, parentId: 4, name:"jonathan" }, { id: 6, isMain: null, parentId: 4, name:"walter" }, { id: 7, isMain: null, parentId: 5, name:"clara" }];
const kid = (p, c) => {
  if (p.hasOwnProperty('kids')) {
    p.kids.push(c);
  } else {
    p.kids = [c];
  }  
};

for (let i = 0; i < table.length - 1; i++) {
  const a = table[i];
  for (let j = i + 1; j < table.length; j++) {
    const b = table[j];
    if (a.id === b.parentId) {
      kid(a, b);
    } else if (b.id === a.parentId) {
      kid(b, a);
    }
  }
}

const result = table.filter((x) => !x.parentId);
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.