0

Stucked right here! How can I iterate this array of objects saving the key of the dates (for example 10/06/2021)? and iterate the skills array of objects?

the array must see like:

 const data = [
 {
 name: 10/06/2021,
 sprint speed: "4.00",
 stamina: "4.00",
 acceleration: "4.00"
 },
 .....
 ....
 ...
 ]

I'm trying with:

Object.keys(element.dates).forEach((date) => {
 if (normalizedSkillsObject[date]) {
 let normalizedTemps = {
 ...normalizedSkillsObject[date],
 ...{ name: date, data: element.dates[date].skills },
 };
 normalizedSkillsObject = {...normalizedSkillsObject, [date]: normalizedTemps}
 } else {
 normalizedSkillsObject = {
 ...normalizedSkillsObject,
 [date]: { name: date, data: element.dates[date].skills.forEach(s => s.name) },
 };
 }
 });

But this is the output:

asked Jun 11, 2021 at 2:22
2
  • 1
    It would be easy to answer if you could add data array and expected output? Commented Jun 11, 2021 at 2:28
  • Do you only care about sprint speed, stamina, and acceleration in your output OR do you only want skills where the avg stat is NOT '0.00'? Commented Jun 11, 2021 at 3:18

4 Answers 4

2

const json = [
 {
 dates: {
 "10/06/2021": {
 average: 4,
 skills: [
 { id: "1", name: "acceleration", avg: "4.00" },
 { id: "2", name: "sprint speed", avg: "4.00" },
 { id: "3", name: "stamina", avg: "4.00" },
 ],
 },
 "13/04/2021": {
 average: 2.97,
 skills: [
 { id: "1", name: "acceleration", avg: "4.00" },
 { id: "2", name: "sprint speed", avg: "4.00" },
 { id: "3", name: "stamina", avg: "4.00" },
 ],
 },
 },
 },
 {
 dates: {
 "20/04/2021": {
 average: 4,
 skills: [
 { id: "1", name: "acceleration", avg: "4.00" },
 { id: "2", name: "sprint speed", avg: "4.00" },
 { id: "3", name: "stamina", avg: "4.00" },
 ],
 },
 },
 },
];
const result = json.flatMap(({ dates }) => {
 return Object.entries(dates).map(([name, value]) =>
 Object.assign({ name }, ...value.skills.map(({ name, avg }) => ({ [name]: avg })))
 );
});
console.log(result);

answered Jun 11, 2021 at 3:04
Sign up to request clarification or add additional context in comments.

Comments

1

const obj = {
 dates: {
 "10/06/2021": {
 skills: [
 { id: 1, name: "accel", avg: "4.0" },
 { id: 2, name: "sprint speed", avg: "4.0" },
 { id: 2, name: "agility", avg: "0.00" },
 ],
 },
 "10/07/2021": {
 average: 4,
 skills: [
 { id: "1", name: "acceleration", avg: "4.00" },
 { id: "2", name: "sprint speed", avg: "4.00" },
 { id: "3", name: "stamina", avg: "4.00" },
 ],
 },
 },
};
const res = Object.entries(obj.dates).reduce(
 (acc, [date, { skills }]) => [
 ...acc,
 {
 name: date,
 ...skills
 .filter(({ avg }) => Number(avg)) // remove skill avg's that are 0
 .reduce((a, { name, avg }) => ({ ...a, [name]: avg }), {}),
 },
 ],
 []
);
console.log(res);

answered Jun 11, 2021 at 2:56

Comments

0

I don't have a way to test this - you didn't give data to work with, but it looks pretty straight forward. Post your data if this isn't right and I'll fine tune

let data = {}
for (const [date, props] of Object.entries(element.dates)) {
 let tmpObj= {name: date};
 props.skills
 .filter( skill => ["sprint speed","stamina","acceleration"].contains(skill.name))
 .forEach(sk => {tmpObj[sk.name] = sk.avg});
 data.push(tmpObj);
}
console.log(data)
answered Jun 11, 2021 at 2:45

4 Comments

I don't believe there is supposed to be any filtering applied
It's only filtering the skills array to get the skills the OP requested in their expected output the array must see like:...
I think I see what you mean. OP was not very clear and I figured they didn't want to type the rest out. Maybe they don't want results that are 0...?
Yep. Well at any rate, OP has a few nearly identical solutions to choose from... :)
-1

const obj = {
 dates: {
 "10/06/2021": {
 skills: [
 { id: 1, name: "accel", avg: "4.0" },
 { id: 2, name: "sprint speed", avg: "4.0" },
 { id: 2, name: "agility", avg: "0.00" },
 ],
 },
 "10/07/2021": {
 average: 4,
 skills: [
 { id: "1", name: "acceleration", avg: "4.00" },
 { id: "2", name: "sprint speed", avg: "4.00" },
 { id: "3", name: "stamina", avg: "4.00" },
 ],
 },
 },
};
const res = Object.entries(obj.dates).reduce(
 (acc, [date, { skills }]) => [
 ...acc,
 {
 name: date,
 ...skills
 .filter(({ avg }) => Number(avg)) // remove skill avg's that are 0
 .reduce((a, { name, avg }) => ({ ...a, [name]: avg }), {}),
 },
 ],
 []
);
console.log(res);

const obj = {
 dates: {
 "10/06/2021": {
 skills: [
 { id: 1, name: "accel", avg: "4.0" },
 { id: 2, name: "sprint speed", avg: "4.0" },
 { id: 2, name: "agility", avg: "0.00" },
 ],
 },
 "10/07/2021": {
 average: 4,
 skills: [
 { id: "1", name: "acceleration", avg: "4.00" },
 { id: "2", name: "sprint speed", avg: "4.00" },
 { id: "3", name: "stamina", avg: "4.00" },
 ],
 },
 },
};
const res = Object.entries(obj.dates).reduce(
 (acc, [date, { skills }]) => [
 ...acc,
 {
 name: date,
 ...skills
 .filter(({ avg }) => Number(avg)) // remove skill avg's that are 0
 .reduce((a, { name, avg }) => ({ ...a, [name]: avg }), {}),
 },
 ],
 []
);
console.log(res);

answered Jun 11, 2021 at 6:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.