过滤对象的嵌套数组

时间:2019-04-11 06:23:19

标签: javascript ecmascript-6

我想要一个数组,该数组返回基于检查属性的某些属性,并且还希望尽可能过滤掉所选技能的水平

AppUtils

该函数返回已检查的技能,但还返回所有具有关卡的对象

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)
            })
          )

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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)

答案 2 :(得分:0)

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

答案 3 :(得分:0)

此外,我们可以通过

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)