2

I would like to id all the objects inside my tree array sequentially. To do this, I need to iterate all the array objects and objects' children, grandchildren etc.. and insert rowId to each.

What I have;

rows={[
  {
    car: 'Audi A4',
    city: 'Las Vegas',
    items: null,
    name: 'Sandra',
    sex: 'Female'
  },
  {
    car: 'Chevrolet Cruze',
    city: 'Tokyo',
    items: [
      {
        car: 'BMW 750',
        city: 'London',
        items: [
          {
            car: 'Toyota Corolla',
            city: 'Chicago',
            name: 'David',
            sex: 'Male'
          },
        ],
        name: 'Robert',
        sex: 'Male'
      }
    ],
    name: 'Sharon',
    sex: 'Female'
  }
]}

What I expect;

rows={[
  {
    car: 'Audi A4',
    city: 'Las Vegas',
    items: null,
    name: 'Sandra',
    sex: 'Female',
    rowId: 1
  },
  {
    car: 'Chevrolet Cruze',
    city: 'Tokyo',
    items: [
      {
        car: 'BMW 750',
        city: 'London',
        items: [
          {
            car: 'Toyota Corolla',
            city: 'Chicago',
            name: 'David',
            sex: 'Male',
            rowId: 2
          },
        ],
        name: 'Robert',
        sex: 'Male',
        rowId: 3
      }
    ],
    name: 'Sharon',
    sex: 'Female',
    rowId: 4
  }
]}

I could do this iteration like this when there is no inner objects;

rows.map((row, index) => ({ ...row, rowId: index }));

Also tried some recursive methods but none of them worked actually neither found something useful on the web.

Looking for a genius developer who can guide me through this. Thanks in advance.

2
  • Do you have to have the numbering go from the inner-most to the outer or can it be the opposite or any direction? Commented Jan 14, 2020 at 8:43
  • I need to number from inner most to the outer like in the sample object. The customized datagrid we use requres that unfortunately. Commented Jan 14, 2020 at 8:46

1 Answer 1

4

You could take a depth-first search algorithm and add id to each object.

const addId = (data, id) => {
        const iter = array => array.forEach(o => {
            if (o.items) iter(o.items); // search first
            o.id = id++;                // add later
        });
        iter(data);
    };
    
var rows = [{ car: 'Audi A4', city: 'Las Vegas', items: null, name: 'Sandra', sex: 'Female' }, { car: 'Chevrolet Cruze', city: 'Tokyo', items: [{ car: 'BMW 750', city: 'London', items: [{ car: 'Toyota Corolla', city: 'Chicago', name: 'David', sex: 'Male' },], name: 'Robert', sex: 'Male' }], name: 'Sharon', sex: 'Female' }];

addId(rows, 1);
   
console.log(rows);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.