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;
Menu