My data consists of an array of objects which look like this:
customer1 = [
{"key": "name",
"value": "Peter"},
{"key": "age",
"value": 23},
{"key": "address",
"value": "xyz St, abcd"},
{"key": "points",
"value": 234}
]
In order to retrieve specific information such as age and address from these objects efficiently, I'm looking for the best approach. In a real scenario, my array may contain anywhere from 20 to 40 key-value pairs, but I might only need to access 5 to 10 values.
Currently, I iterate through the object using conditions to extract and assign values to variables. However, this method involves writing multiple else-if statements (5-10).
Here is an example of what I currently do:
let name: string;
let points: number;
for (var item of customer1) {
if (item.key === "name") {
name = item.value;
} else if (item.key === "points") {
points = item.value;
}};