我想要一个数组,该数组返回基于检查属性的某些属性,并且还希望尽可能过滤掉所选技能的水平
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)
})
)
答案 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)