在嵌套的对象数组中过滤

问题描述 投票:1回答:4

我想要一个数组,它返回已检查属性的某些属性,并希望尽可能筛选出所选技能的级别

const data = [
      {
        "name": "Beginner",
        "skills": [
          {
            "name": "Skill 1",
            "checked": false
          },
          {
            "name": "Skill 2",
            "checked": true
          }
        ]
      },
      {
        "name": "Intermediate",
        "skills": [
          {
            "name": "Skill 1",
            "checked": true
          },
          {
            "name": "Skill 2",
            "checked": false
          }
        ]
      },
      {
        "name": "Professional",
        "skills": [
          {
            "name": "Skill 1",
            "checked": false
          },
          {
            "name": "Skill 2",
            "checked": false
          }
        ]
      },
    ]

该函数返回已检查的技能,但也返回所有级别对象

levelwithskills = data.map(level =>
              Object.assign({}, level, {
              skills: level.skills.filter(skill => skill.checked)
            })
          )

我希望得到格式的结果

   [
      {
        "name": "Beginner",
        "skills": [
          {
            "name": "Skill 2",
            "checked": true
          }
        ]
      },
      {
        "name": "Intermediate",
        "skills": [
          {
            "name": "Skill 1",
            "checked": true
          }
        ]
      }
    ]
javascript ecmascript-6
4个回答
3
投票

映射后,filter是否有任何skills项目:

const data = [{
    "name": "Beginner",
    "skills": [{
        "name": "Skill 1",
        "checked": false
      },
      {
        "name": "Skill 2",
        "checked": true
      }
    ]
  },
  {
    "name": "Intermediate",
    "skills": [{
        "name": "Skill 1",
        "checked": true
      },
      {
        "name": "Skill 2",
        "checked": false
      }
    ]
  },
  {
    "name": "Professional",
    "skills": [{
        "name": "Skill 1",
        "checked": false
      },
      {
        "name": "Skill 2",
        "checked": false
      }
    ]
  },
]
levelwithskills = data
  .map(level => ({ ...level, skills: level.skills.filter(skill => skill.checked) }))
  .filter(({ skills }) => skills.length)
console.log(levelwithskills);

2
投票

你可以用Array#reduce方法做到这一点。

const levelwithskills = data.reduce((arr, level) => {
  // filter skills
  let skills = level.skills.filter(({ checked }) => checked);
  // check length of filtered skills if greater than 0 then push into array
  skills.length && arr.push({ ...level, skills });
  // return array reference
  return arr
  // set initial value as an empty array for the result
}, [])

const data = [{
    "name": "Beginner",
    "skills": [{
        "name": "Skill 1",
        "checked": false
      },
      {
        "name": "Skill 2",
        "checked": true
      }
    ]
  },
  {
    "name": "Intermediate",
    "skills": [{
        "name": "Skill 1",
        "checked": true
      },
      {
        "name": "Skill 2",
        "checked": false
      }
    ]
  },
  {
    "name": "Professional",
    "skills": [{
        "name": "Skill 1",
        "checked": false
      },
      {
        "name": "Skill 2",
        "checked": false
      }
    ]
  },
];

const levelwithskills = data.reduce((arr, level) => {
  let skills = level.skills.filter(({ checked }) => checked);
  skills.length && arr.push({ ...level, skills });
  return arr
}, [])

console.log(levelwithskills)

1
投票

const data = [
      {
        "name": "Beginner",
        "skills": [
          {
            "name": "Skill 1",
            "checked": false
          },
          {
            "name": "Skill 2",
            "checked": true
          }
        ]
      },
      {
        "name": "Intermediate",
        "skills": [
          {
            "name": "Skill 1",
            "checked": true
          },
          {
            "name": "Skill 2",
            "checked": false
          }
        ]
      },
      {
        "name": "Professional",
        "skills": [
          {
            "name": "Skill 1",
            "checked": false
          },
          {
            "name": "Skill 2",
            "checked": false
          }
        ]
      },
    ]
    
    let newData = data.map(d=>({
      ...d,
      skills: d.skills.filter(fd=>fd.checked)
    })).filter(afd=>afd.skills.length)
    console.log(newData);

1
投票

此外,我们可以这样做

const data = [
      {
        "name": "Beginner",
        "skills": [
          {
            "name": "Skill 1",
            "checked": false
          },
          {
            "name": "Skill 2",
            "checked": true
          }
        ]
      },
      {
        "name": "Intermediate",
        "skills": [
          {
            "name": "Skill 1",
            "checked": true
          },
          {
            "name": "Skill 2",
            "checked": false
          }
        ]
      },
      {
        "name": "Professional",
        "skills": [
          {
            "name": "Skill 1",
            "checked": false
          },
          {
            "name": "Skill 2",
            "checked": false
          }
        ]
      },
    ]




let filteredData=[]
data.map((obj)=>{
  obj.skills.map((innerObj)=>{
      if(innerObj.checked===true){
  filteredData.push({name:obj.name,
                  skills:innerObj})
      } 
  })
})
 

  console.log(filteredData)

推荐问答