How to filter this array? Algorithm

let arr = [
        {
            name: "tom",
            children: [
                {name: "tomson1", age: 11},
                {name: "tomson2", age: 12}
            ]
        },
        {
            name: "tony",
            children: [
                {name: "tonyon1", age: 13},
                {name: "tonyon2", age: 14},
                {name: "tonyon3", age: 15}
            ]
        },
        {
            name: "mike",
            children: [
                {name: "mikeson1", age: 16},
                {name: "mikeson2", age: 17}
            ]
        }
    ]
    let list = [{name: "tom"},{name: "tony"}, {name: "tonyon1"}]
arrlistlistlistsonchildren



```
[
    {
        name: "tom"
    },
    {
        name: "tony",
        children: [
            {name: "tonyon1", age: 13}
        ]
    }
]
```
Nov.02,2021

let arr = [{
        name: 'tom',
        children: [{
                name: 'tomson1',
                age: 11
            },
            {
                name: 'tomson2',
                age: 12
            }
        ]
    },
    {
        name: 'tony',
        children: [{
                name: 'tonyon1',
                age: 13
            },
            {
                name: 'tonyon2',
                age: 14
            },
            {
                name: 'tonyon3',
                age: 15
            }
        ]
    },
    {
        name: 'mike',
        children: [{
                name: 'mikeson1',
                age: 16
            },
            {
                name: 'mikeson2',
                age: 17
            }
        ]
    }
]
let list = [{
    name: 'tom'
}, {
    name: 'tony'
}, {
    name: 'tonyon1'
}]
let newArr = []
for (let j in list) {
    for (let i in arr) {
        if (arr[i].name == list[j].name) {
            if (arr[i].children.length > 0) {
                newArr.push(arr[i])

            }
        }
    }
}
console.log('newArr: ', newArr);

newArr is the array you want?


first convert list into a two-dimensional array [{name:xxx}, {name:xxxson}] = > [[xxx,xxxson]]
convert arr to an object [{name:xxx,children: [{name:xxxson}]}] = > {xxx: {children: []}} / / you can also omit this step
, and then construct the result


const mlist = new Map()
for (const { name } of list) {
  const m = /^(\S+?)s?on\d+$/.exec(name)
  const key = m ? m[1] : name
  const cset = mlist.get(key) || new Set()
  if (m) { cset.add(name) }
  mlist.set(key, cset)
}

const newArr = arr
  .map(({ name, children }) => {
    const cset = mlist.get(name)
    if (!cset) { return null }
    children = cset.size <= 0 || !children 
      ? []
      : children.filter(({ name }) => cset.has(name))
    return children.length > 0
      ? { name, children }
      : { name }
  })
  .filter(Boolean)
based on the two-dimensional array converted by list.
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-1b32fa6-4eabf.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-1b32fa6-4eabf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?