Reputation: 3543
I have an object like this:
const Media = {
references: { analyze: [432, 5], translate: ["string", false] },
extensions: { analyze: ["something"], translate: ["something here"] },
words: ["a word"],
brackets: [],
pronounce: [],
instructions: {
Expand_Explain: ['Before_First_Movie_Watch_Explain', 'Before_First_Movie_Watch_Explain2', 'Before_First_Movie_Watch_Explain3'],
Hot_Tutorial: ['1', 'some element', 2],
Next: [54, true, "string"],
}
}
And I want to create another object with only instructions
property with empty arrays inside:
So this is the desired result:
const NewEmptyMedia = {
instructions: {
Expand_Explain: [],
Hot_Tutorial: [],
Next: [],
}
}
Note: the pattern is the same for instructions
property always but the number of properties inside instructions
is variable.
I created a loop to do this but I need to check multiple if statements and it's really ugly...
Upvotes: 0
Views: 151
Reputation: 6742
You can use a class, like this
const Media = {
references: { analyze: [432, 5], translate: ["string", false] },
extensions: { analyze: ["something"], translate: ["something here"] },
words: ["a word"],
brackets: [],
pronounce: [],
instructions: {
Expand_Explain: ['Before_First_Movie_Watch_Explain', 'Before_First_Movie_Watch_Explain2', 'Before_First_Movie_Watch_Explain3'],
Hot_Tutorial: ['1', 'some element', 2],
Next: [54, true, "string"],
}
}
class EmptyMedia {
constructor(template) {
this.instructions = {};
for (const arr in template.instructions) {
this.instructions[arr] = [];
}
}
}
console.log(new EmptyMedia(Media));
Upvotes: 0
Reputation: 1523
You can use a reducer on Object.keys for Media.instructions.
Something like this should suffice:
Object.keys(Media.instructions).reduce((acc, key) => ({...acc, [key]: []}), {});
Upvotes: 2