Home > Mobile >  Convert node into one array in Javascript
Convert node into one array in Javascript

Time:09-21

I have a array like this :

const data = [
    {
        key: 1010,
        children: [
            { key: 10101, children: [] },
            {
                key: 10102,
                children: [
                    {
                        key: 1010101,
                        checked: false,
                    },
                    {
                        key: 1010102,
                        checked: false,
                    },
                ],
            },
        ],
    },
    {
        key: 2020,
        children: [
            {
                key: 20201,
                children: [
                    {
                        key: 202020,
                        checked: false,
                    },
                    {
                        key: 202021,
                        checked: false,
                    },
                ],
            },
        ],
    },
];

And I want to get a output like this :

const output = {
    idsGrid: [
        {
            gridId: 10101,
            isTable: false,
        },
        {
            gridId: 10102,
            isTable: false,
        },
        {
            gridId: 1010101,
            isTable: true,
        },
        {
            gridId: 1010102,
            isTable: true,
        },
        {
            gridId: 20201,
            isTable: false,
        },
        {
            gridId: 202020,
            isTable: true,
        },
        {
            gridId: 202021,
            isTable: true,
        },
    ],
};

Actually, I want to map into my seconds level's arrays and extract all object and push them into only One array, with IsTable property (WHEN checked is false ==> IsTable is TRUE)

I'm trying using recursive method but it's not working right now..

This is what I'm trying :

console.log('data ==>',data)
let a = []
const v = data.map(e => {
  return e.children.map(child => {
   return  a.push({key : child.key,isTable : true})
  })
} )

CodePudding user response:

Let's iterate the object. Or more exactly the array of objects and their children. We keep track of level so as not to include first level.

const data=[{key:1010,children:[{key:10101,children:[]},{key:10102,children:[{key:1010101,checked:!1},{key:1010102,checked:!1}]}]},{key:2020,children:[{key:20201,children:[{key:202020,checked:!1},{key:202021,checked:!1}]}]}];

function do_it(data) {
  var result = [];

  function flatten_array(arr, level) {
    level = level || 0;

    arr.forEach(function(child) {
      if (level) {
        result.push({
          gridId: child.key,
          isTable: child.checked === false
        });
      }
      child.children && flatten_array(child.children, level 1)
    });
  }

  flatten_array(data)

  return {
    idsGrid: result
  }
}

var result = do_it(data);
console.log(result);
.as-console-wrapper {
  max-height: 100% !important
}

CodePudding user response:

You could do something like that :


public convert(data: any[]) {
    const res: { key?: number; table?: boolean; }[] = [];
    data.forEach((obj) => {
        const temp = {
            gridId: null,
            table: false,
        }
        Object.keys(obj).forEach((k) => {
            switch (k) {
                case 'key':
                    temp.gridId = obj[k];
                    break;
                case 'children':
                    temp.table = true;
                    const tempRes = this.convert(obj[k]);
                    tempRes.forEach((temp) => res.push(temp));
                default:
                    break;
            }
            res.push(temp);
        })
    })
    return new Set(res);
}

Which will ouput :
enter image description here

So it's not exactly what you want since it adds a value field.

  • Related