Javascript 是否从键/值对象中的值创建数组?
我有一系列不同的练习:Javascript 是否从键/值对象中的值创建数组?,javascript,Javascript,我有一系列不同的练习: exercises: { push: ['Push up', 'Bench press'], legs: ['Squat', 'Power squats'], pull: ['Pull up', 'Chin up'], cardioCore: ['Running high knees', 'Plank'] } 如何将所有这些合并到单个数组或对象中?我需要创建以下内容: allExercises: ['Push up', 'Bench press', 'S
exercises: {
push: ['Push up', 'Bench press'],
legs: ['Squat', 'Power squats'],
pull: ['Pull up', 'Chin up'],
cardioCore: ['Running high knees', 'Plank']
}
如何将所有这些合并到单个数组或对象中?我需要创建以下内容:
allExercises: ['Push up', 'Bench press', 'Squat', 'Power squats', 'Pull up', 'Chin up', 'Running high knees', 'Plank']
我还将按字母顺序排序,因此顺序并不重要
我想我可以用一个forEach
,类似这样的:
let allExercises = [];
exercises.forEach(exerciseGroup=>{
allExercises.push(exerciseGroup);
});
但这感觉有点乱。有更好的ES6解决方案吗 是的,只要使用和
是的,只要使用和
我丑陋的解决方案:
let combineExercises = [];
Object.values(allExercises).forEach((exerciseGroup) => {
exerciseGroup.forEach((exercise) => {
combineExercises.push(exercise)
})
});
console.log(combineExercises);
我丑陋的解决方案:
let combineExercises = [];
Object.values(allExercises).forEach((exerciseGroup) => {
exerciseGroup.forEach((exercise) => {
combineExercises.push(exercise)
})
});
console.log(combineExercises);
您可以使用
Object.values
&spread运算符
var练习={
推压:[“推压”、“台压”],
腿:[“蹲下”,“力量蹲下],
拉:[“拉起”、“抬起头来”],
cardioCore:[“高膝跑”、“木板跑”]
}
var allexercise={},tempArray=[];
for(练习中的var键){
tempArray.push(…Object.values(练习[keys]))
}
allexercise=tempArray;
console.log(allexercise)
您可以使用对象.值
和扩展运算符
var练习={
推压:[“推压”、“台压”],
腿:[“蹲下”,“力量蹲下],
拉:[“拉起”、“抬起头来”],
cardioCore:[“高膝跑”、“木板跑”]
}
var allexercise={},tempArray=[];
for(练习中的var键){
tempArray.push(…Object.values(练习[keys]))
}
allexercise=tempArray;
console.log(allexercise)
还有另一种方法,与ES6兼容:
Object.getOwnPropertyNames(exercises)
.reduce((allExercises, exerciseName) => [...allExercises, ...(exercises[exerciseName])], [])
正如@stealththeninja所说,您也可以这样做来减少大量开销
Object.getOwnPropertyNames(exercises).reduce((allExercises, exerciseName) => {
allExercises.push(...exercises[exerciseName]);
return allExercises;
}, []);
还有另一种方法,ES6兼容:
Object.getOwnPropertyNames(exercises)
.reduce((allExercises, exerciseName) => [...allExercises, ...(exercises[exerciseName])], [])
正如@stealththeninja所说,您也可以这样做来减少大量开销
Object.getOwnPropertyNames(exercises).reduce((allExercises, exerciseName) => {
allExercises.push(...exercises[exerciseName]);
return allExercises;
}, []);
.join(',).split(',')
需要什么?.join
将嵌套数组当作平面数组进行连接<代码>[[1,2,3],[2,3,4,5]。连接(',')将是1,2,3,4,5
@Xufox yes将不起作用,如果练习中有逗号,Xufox将在连接/拆分失败的情况下引发有效的测试用例。我们可能会使用(…
),但它仍然不必要地比使用对象值和数组reduce更复杂。最后一种方法实际上非常优雅。.join(',).split(',)
?。join
像平面一样连接嵌套数组<代码>[[1,2,3],[2,3,4,5]。连接(',')将是1,2,3,4,5
@Xufox yes将不起作用,如果练习中有逗号,Xufox将在连接/拆分失败的情况下引发有效的测试用例。我们可能会使用(..
),但它仍然比使用对象#值和数组#减少不必要地复杂。最后一种方法实际上非常优雅。个人偏好,但除了.concat
函数之外,还可以使用..
(扩展)运算符,也就是对象。值
是ES6或ES7的一部分,只是curious@AyushGupta嗯…它实际上是ES8,但它可以很容易地被多填充或替换为对象。keys
方法。是的,我很好奇,因为这个调用曾经在节点6.10
xDPersonal preference中失败过,但是除了.concat
函数之外,还可以使用…
(扩展)操作符,是对象。值是ES6或ES7的一部分,只是curious@AyushGupta嗯…它实际上是ES8,但它可以很容易地被多填充或替换为对象。keys
方法。是的,我很好奇,因为这个调用曾经在节点6.10中失败过xDUse.concat()
。使用.concat()
[…all[名称]
每次返回一个新数组。Reduce通过引用传递对象(或数组),为什么不添加项并返回all
?all.push(…练习[名称]);全部返回;
你可以,我只是更喜欢排列方式。一行返回看起来更干净。一行返回看起来更干净。
语法更干净,但为每个属性名称创建一个新数组是浪费时间的。嗯,是的。编辑了答案以添加你的建议作为替代,谢谢!谢谢考虑反馈,我很感激it.:[…all,…exercises[name]
每次返回一个新数组。Reduce通过引用传递对象(或数组),为什么不添加项并返回all
?all.push(…exercises[name]);全部返回;
你可以,我只是更喜欢排列方式。一行返回看起来更干净。一行返回看起来更干净。
语法更干净,但为每个属性名称创建一个新数组是浪费时间的。嗯,是的。编辑了答案以添加你的建议作为替代,谢谢!谢谢考虑反馈,我很感激它是……)