How the map of js recursively retrieves data gracefully

[
  {
    name: "123",
    type: "menu",
    children: [{ name: "321", type: "operation", children: null }],
  },
  {
    name: "456",
    type: "menu",
    children: [
      {
        name: "654",
        type: "menu",
        children: [{ name: "546", type: "operation", children: null }],
      },
    ],
  },
];

such a set of data

I want to take out all the name of type = "operation" through the map of js. For example, if the data is taken out as above, the result is

.
["321","546"]

Note: the actual data may be nested in the wireless layer

Jul.13,2021

const getNamesByType = (target, init = []) => {
    target.forEach(item => {
        item.type === 'operation' && init.push(item.name);
        item.children && getNamesByType(item.children, init);
    });
    return init;
};

getNamesByType(yourTarget);

clipboard.png

const defaultFn = o => o
const recursion = function recursion (obj, children = 'children', convertor = defaultFn, parent) {
    if (Array.isArray(obj)) {
        return obj.map(item => recursion(item, children, convertor))
    } else if (obj[children]) {
        obj[children] = obj[children].map(item => recursion(item, children, convertor, obj))
    }
    return convertor(obj, parent)
}

const data = [
  {
    name: '123',
    type: 'menu',
    children: [{ name: '321', type: 'operation', children: null }],
  },
  {
    name: '456',
    type: 'menu',
    children: [
      {
        name: '654',
        type: 'menu',
        children: [{ name: '546', type: 'operation', children: null }],
      },
    ],
  },
];

let res = [];
recursion(data, 'children', function (item) {
  if (item.type === 'operation') {
    res.push(item.name)
  }
})

res;
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a572-41248.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a572-41248.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?