In my current code, I have functions that retrieve specific properties from an object within an array.
Since each object in the array has multiple properties, I find myself using separate functions to extract different properties. This approach is not efficient and there must be a better way to accomplish this task.
var fiftyplanets = [{
"Hostname": "11 Com",
"Distance [pc]": 110.62,
"Effective Temperature [K]": 4742,
"Date of Last Update": "5/14/2014"
}];
function findProperty(hostname, property) {
function search(am, im) {
if (am.Hostname === hostname) {
index = im;
return true;
}
}
var index;
if (fiftyplanets.some(search)) {
return fiftyplanets[index][property];
}
}
var name = value;
var result = fiftyplanets.indexOf(name);
var distance = findProperty(name, 'Distance [pc]');
var temperature = findProperty(name, 'Effective Temperature [K]');
I am looking for a more efficient way to write a single function that can search for specific properties within the objects in the array. Is there a better approach to achieve this?