js 两层数组嵌套,根据字段进行字段过滤

A马大傻
  • 7

问题描述

如代码所示,现有一两层嵌套的数据,根据sort字段不同,sort为0的有children,因为列表字段超级多,直接发给后台会因为长度问题报错,所以需要对整个数据及sort为0的children数组进行字段过滤,过滤后的数据结构如图2。(即只有id,sort,children字段是有效字段)

相关代码

元数据:
`
list:[

{
    id:"1",
    sort:"0",
    type:"radio",
    title:"这是大题",
    children:[
        {
            id:"1",
            sort:"1",
            type:"radio",
            title:"这是大题",
        },
        {
            id:"1",
            sort:"2",
            type:"radio",
            title:"这是单选",
        }
    ]
},
{
    id:"1",
    sort:"1",
    type:"radio",
    title:"这是大题",
    children:[]//只有sort为0时才有children,其他情况都为空
}

]
`
图2(target):
`
list:[

{
    id:"1",
    sort:"0",
    children:[
        {
            id:"1",
            sort:"1",
        },
        {
            id:"1",
            sort:"2",
        }
    ]
}

]
`

回复
阅读 3.6k
6 个回答
✓ 已被采纳
  const resultData = list.map(item => {
        let result = { id: item.id, sort: item.sort, children: [] }
        if (item.sort === '0') {
            result.children = item.children.map(ele => {
                return { id: ele.id, sort: ele.sort }
            })
        }
        return result
    })
leonleung
  • 2.9k
let ori = [
  {
    id: "1",
    sort: "0",
    type: "radio",
    title: "这是大题",
    children: [
      {
        id: "1",
        sort: "1",
        type: "radio",
        title: "这是大题",
      },
      {
        id: "1",
        sort: "2",
        type: "radio",
        title: "这是单选",
      }
    ]
  },
  {
    id: "1",
    sort: "1",
    type: "radio",
    title: "这是大题",
    children: []//只有sort为0时才有children,其他情况都为空
  }
]

let res = []

let objFilter = obj => {
  return {
    id: obj.id,
    sort: obj.sort
  }
}

ori.forEach(first => {
  if (first.sort !== '0') return
  let temp = objFilter(first)
  temp.children = []
  first.children.forEach(second => {
    temp.children.push(objFilter(second))
  })
  res.push(temp)
})

console.log(res)
function resetData(list){
  return list.map(function(item){
    let obj = {
      id: item.id,
      sort: item.sort
    }
    if(item.children && item.children.length){
      obj.children = resetData(item.children)
    }
    return obj
  })
}

list = list.filter(function(item){return item.sort === '0'})
console.log(resetData(list))

let res = arr.map(({sort, id,children}) => ({ id, sort,children }))

res.map((item,index)=>{

if(item.sort == 0){ 
    item.children = item.children.map(({sort, id}) => ({ id, sort }))
}

})
console.log(res)

我是 lodash 重度患者,所以就上个 lodash 的版本哈

var a = [{
    id:"1",
    sort:"0",
    type:"radio",
    title:"这是大题",
    children:[
        {
            id:"1",
            sort:"1",
            type:"radio",
            title:"这是大题",
        },
        {
            id:"1",
            sort:"2",
            type:"radio",
            title:"这是单选",
        }
    ]
},
{
    id:"1",
    sort:"1",
    type:"radio",
    title:"这是大题",
    children:[]//只有sort为0时才有children,其他情况都为空
}]
_.map(a, item => {
  let result;
  item.children = _.map(item.children, cItem => _.pick(cItem, ["id", "sort"]));
  result = _.pick(item, ["id", "sort", "children"]);
  return result;
});

image.png

本人的复杂解答:

var newArray = []
list.forEach((item)=>{
    if(item.sort == 0){
        var arr = []
        item.children.forEach((it)=>{
            arr.push({
                id:it.id,
                sort:it.sort
            })
        })
        item.children = arr
        newArray.push({
            id:item.id,
            sort:item.sort,
            children:item.children
        })
    } else {
        newArray.push({
            id:item.id,
            sort:item.sort,
            children:[]
        })
    }
})
console.log(newArray)
宣传栏