Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  194] [ 1]  / answers: 1 / hits: 69736  / 4 Years ago, tue, july 14, 2020, 12:00:00

I am new to Typescript and I need to iterate over a Record type make some updates to the values and return the Record.


This is how the types are defined:


type Parent = Readonly<Record<string, Children>>;
type Children = ReadonlyArray<string>;

Here is some sample data I would like to iterate over:


const data = {
parent1: ["child1", "child2"],
parent2: ["child1","child2"]
};

Method to update values in record:


const updateChildren = (child: Children) : Children => {
return child.map( value => value + 'updated');
}

I am struggling to write the syntax for it, tried to look for examples but couldn't find anything helpful.


I am able to iterate over the record using Object.entries


Object.entries(data).forEach(([key, value]) => console.log(key, value));

I also tried to use Object.keys


Object.keys(data)
.map(key => updateChildren(data[key]))

I guess I am close but not sure how to return the map as here its returning Array [Array]


Is there some nice way to iterate do the update and it would return the updated data in same type used.


Thanks for reading.


Here is the javascript snippet of what I am trying to do and get the updatedMap in example below.




const data = {
parent1: [child1, child2],
parent2: [child1,child2]
};

function updateChildren(children) {
return children.map(child => child+'updated');
}

const updatedMap = new Map();

for (const [key, value] of Object.entries(data)) {
updatedMap.set(key, updateChildren(value));
}

updatedMap.forEach((value, key) => console.log(key + : + value));


console.log(Object.keys(data).map(key => updateChildren(data[key])));




More From » typescript

 Answers
2

Something like this...


type Children = ReadonlyArray<string>;

const data: Parent = {
parent1: ["child1", "child2"],
parent2: ["child1", "child2"],
};


type MutableObject<T> = { -readonly [P in keyof T]: T[P] };

const updateChildren = (child: Children): Children => {
return child.map(value => value + 'updated');
}

let newObj: Parent = Object.entries(data).reduce<MutableObject<Parent>>((acc, cur) => {
acc[cur[0]] = updateChildren(cur[1]);
return acc;
}, {})

console.log(newObj)


[#50797] Wednesday, July 8, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billosvaldor

Total Points: 601
Total Questions: 113
Total Answers: 113

Location: Iceland
Member since Sat, Sep 17, 2022
2 Years ago
;