JavaScript 数组频率测量对象属性
JavaScript array frequency meaasure object properties
我正在处理一些我似乎无法找到解决方案的逻辑。我希望你能帮我解决这个问题。我有以下数组:
const exampleDay = [
exercise.prehab.jointRotations,
exercise.prehab.upperbody,
exercise.skillTechnique.planche,
exercise.skillTechnique.HSPU,
exercise.strengthPrimary.upperbody.horPress,
exercise.strengthSecondary.upperbody.incPress,
exercise.strengthPrimary.upperbody.verPress,
exercise.strengthIsolation.upperbody.biceps,
exercise.strengthIsolation.upperbody.sideDelts,
exercise.strengthIsolation.upperbody.rearDelts,
exercise.mobility.traction
];
我在这个数组中引用的对象可以在这里找到:
const exercises = {
prehab: {
jointRotations: <p className="prehab">Joint Rotations</p>,
upperbody: <p className="prehab">Upper Body Prehab</p>,
lowerbody: <p className="prehab">Lower Body Prehab</p>,
scaps: <p className="prehab">Scaps</p>
},
skillTechnique: {
core: <p className="skill">Core</p>,
frontLever: <p className="skill">Front Lever Technique</p>,
planche: <p className="skill">Planche Technique</p>,
HSPU: <p className="skill">HSPU Technique</p>
},
strengthPrimary: {
upperbody: {
horPress: <p className="strength">Horizontal Press</p>,
incPress: <p className="strength">Incline Press</p>,
verPress: <p className="strength">Vertical Press</p>,
horPull: <p className="strength">Horizontal Pull</p>,
verPull: <p className="strength">Vertical Pull</p>
},
lowerbody: {
quad: <p className="strength">Quad</p>,
hamGlute: <p className="strength">Ham/Glute</p>
}
},
strengthSecondary: {
upperbody: {
horPress: <p className="strength">Horizontal Press Sec</p>,
incPress: <p className="strength">Incline Press Sec</p>,
verPress: <p className="strength">Vertical Press Sec</p>,
horPull: <p className="strength">Horizontal Pull Sec</p>,
verPull: <p className="strength">Vertical Pull Sec</p>
},
lowerbody: {
quad: <p className="strength">Quad Sec</p>,
hamGlute: <p className="strength">Ham/Glute Sec</p>
}
},
strengthIsolation: {
upperbody: {
elbow: <p className="strength">Elbow Prep</p>,
sideDelts: <p className="strength">Side Delts</p>,
rearDelts: <p className="strength">Rear Delts</p>,
biceps: <p className="strength">Biceps</p>
},
lowerbody: {
quad: <p className="strength">Quads</p>,
hamstrings: <p className="strength">Hamstrings</p>,
calves: <p className="strength">Calves</p>
}
},
mobility: {
traction: <p className="mobility">Mobility 1 Traction</p>,
tension: <p className="mobility">Mobility 2 Tension</p>
}
};
我想制作一个函数来测量数组中 exercise.prehab、exercise.skillTechnique 数量的频率。例如,此函数将 return 一个数组,如下所示:
[{prehab: 2}, {skillTechnique: 2}, {strengthPrimary: 2}, {strengthSecondary: 1}, {strengthIsolation: 3}, {mobility: 1}]
我不确定如何从我现在创建的数组中访问父对象 属性。有任何想法吗?提前致谢!
I want to make a function that can measure the frequency of the amount of exercise.prehab
, exercise.skillTechnique
in my array.
你不能,在一般情况下可靠。
I'm not sure how I can access a parent object property from the array that I made now.
你不能。数组中的内容只是 exercise.prehab.jointRotations
(例如)在您创建数组时所具有的值。 数组中的值与 exercise.prehab
对象中的值之间没有正在进行的 link。
现在,如果所有值都是唯一的(看起来它们可能是唯一的),您可以使用如下函数寻找它们:
function findParent(data, targetValue) {
for (const value of Object.values(data)) {
if (value === targetValue) {
return data;
}
if (value && typeof value === "object") {
const found = findParent(value, targetValue);
if (found) {
return found;
}
}
}
return null; // Not found
}
这是一个递归搜索您传递给它的对象以获得目标值的函数。所以你可以通过 findParent(exercises, exampleDay[0])
.
找到 exampleDay[0]
的父对象(如果有的话)
但是一旦有任何重复,那将不起作用。无论如何,这很尴尬(如果有 很多),很慢。
相反,我建议将父对象及其 属性 存储在数组中:
const exampleDay = [
{source: exercise.prehab, prop: "jointRotations"},
// ...
];
这样,您就有了源对象,属性 值就来自手头的对象。不过,鉴于您正在寻找的总数,您可能需要更进一步(但假设您可以假设 exercise
部分):
const exampleDay = [
{source: "prehab", prop: "jointRotations"},
// ...
];
(我没有充分考虑 strenghtIsolation
有一个额外的水平,但你可以根据需要进行调整。)
当你需要这个值时,使用exercise[source][prop]
来获取它。当你想知道一个给定的源对象被使用了多少次时,就变得容易多了:
let uses = Object.create(null);
for (const {source} of exampleDay) {
uses[source] = (uses[source] || 0) + 1;
}
uses = Object.entries(uses).map(([name, count]) => ({[name]: count}));
我正在处理一些我似乎无法找到解决方案的逻辑。我希望你能帮我解决这个问题。我有以下数组:
const exampleDay = [
exercise.prehab.jointRotations,
exercise.prehab.upperbody,
exercise.skillTechnique.planche,
exercise.skillTechnique.HSPU,
exercise.strengthPrimary.upperbody.horPress,
exercise.strengthSecondary.upperbody.incPress,
exercise.strengthPrimary.upperbody.verPress,
exercise.strengthIsolation.upperbody.biceps,
exercise.strengthIsolation.upperbody.sideDelts,
exercise.strengthIsolation.upperbody.rearDelts,
exercise.mobility.traction
];
我在这个数组中引用的对象可以在这里找到:
const exercises = {
prehab: {
jointRotations: <p className="prehab">Joint Rotations</p>,
upperbody: <p className="prehab">Upper Body Prehab</p>,
lowerbody: <p className="prehab">Lower Body Prehab</p>,
scaps: <p className="prehab">Scaps</p>
},
skillTechnique: {
core: <p className="skill">Core</p>,
frontLever: <p className="skill">Front Lever Technique</p>,
planche: <p className="skill">Planche Technique</p>,
HSPU: <p className="skill">HSPU Technique</p>
},
strengthPrimary: {
upperbody: {
horPress: <p className="strength">Horizontal Press</p>,
incPress: <p className="strength">Incline Press</p>,
verPress: <p className="strength">Vertical Press</p>,
horPull: <p className="strength">Horizontal Pull</p>,
verPull: <p className="strength">Vertical Pull</p>
},
lowerbody: {
quad: <p className="strength">Quad</p>,
hamGlute: <p className="strength">Ham/Glute</p>
}
},
strengthSecondary: {
upperbody: {
horPress: <p className="strength">Horizontal Press Sec</p>,
incPress: <p className="strength">Incline Press Sec</p>,
verPress: <p className="strength">Vertical Press Sec</p>,
horPull: <p className="strength">Horizontal Pull Sec</p>,
verPull: <p className="strength">Vertical Pull Sec</p>
},
lowerbody: {
quad: <p className="strength">Quad Sec</p>,
hamGlute: <p className="strength">Ham/Glute Sec</p>
}
},
strengthIsolation: {
upperbody: {
elbow: <p className="strength">Elbow Prep</p>,
sideDelts: <p className="strength">Side Delts</p>,
rearDelts: <p className="strength">Rear Delts</p>,
biceps: <p className="strength">Biceps</p>
},
lowerbody: {
quad: <p className="strength">Quads</p>,
hamstrings: <p className="strength">Hamstrings</p>,
calves: <p className="strength">Calves</p>
}
},
mobility: {
traction: <p className="mobility">Mobility 1 Traction</p>,
tension: <p className="mobility">Mobility 2 Tension</p>
}
};
我想制作一个函数来测量数组中 exercise.prehab、exercise.skillTechnique 数量的频率。例如,此函数将 return 一个数组,如下所示:
[{prehab: 2}, {skillTechnique: 2}, {strengthPrimary: 2}, {strengthSecondary: 1}, {strengthIsolation: 3}, {mobility: 1}]
我不确定如何从我现在创建的数组中访问父对象 属性。有任何想法吗?提前致谢!
I want to make a function that can measure the frequency of the amount of
exercise.prehab
,exercise.skillTechnique
in my array.
你不能,在一般情况下可靠。
I'm not sure how I can access a parent object property from the array that I made now.
你不能。数组中的内容只是 exercise.prehab.jointRotations
(例如)在您创建数组时所具有的值。 数组中的值与 exercise.prehab
对象中的值之间没有正在进行的 link。
现在,如果所有值都是唯一的(看起来它们可能是唯一的),您可以使用如下函数寻找它们:
function findParent(data, targetValue) {
for (const value of Object.values(data)) {
if (value === targetValue) {
return data;
}
if (value && typeof value === "object") {
const found = findParent(value, targetValue);
if (found) {
return found;
}
}
}
return null; // Not found
}
这是一个递归搜索您传递给它的对象以获得目标值的函数。所以你可以通过 findParent(exercises, exampleDay[0])
.
exampleDay[0]
的父对象(如果有的话)
但是一旦有任何重复,那将不起作用。无论如何,这很尴尬(如果有 很多),很慢。
相反,我建议将父对象及其 属性 存储在数组中:
const exampleDay = [
{source: exercise.prehab, prop: "jointRotations"},
// ...
];
这样,您就有了源对象,属性 值就来自手头的对象。不过,鉴于您正在寻找的总数,您可能需要更进一步(但假设您可以假设 exercise
部分):
const exampleDay = [
{source: "prehab", prop: "jointRotations"},
// ...
];
(我没有充分考虑 strenghtIsolation
有一个额外的水平,但你可以根据需要进行调整。)
当你需要这个值时,使用exercise[source][prop]
来获取它。当你想知道一个给定的源对象被使用了多少次时,就变得容易多了:
let uses = Object.create(null);
for (const {source} of exampleDay) {
uses[source] = (uses[source] || 0) + 1;
}
uses = Object.entries(uses).map(([name, count]) => ({[name]: count}));