Users have defined an array of strings with different combinations:
pickedStrings = ["A", "B", "G", "L", "Y"];
This can also be:
pickedStrings = ["A", "G"] or pickedStrings = ["A", "L", "Y"]
or any other mix. More strings may be added in the future.
These strings help identify objects in a mongo document by looping through the array and querying the objects.
var pickedArray, fLen, pickedDataArray, i;
pickedArray = pickedStrings;
fLen = pickedStrings.length;
pickedDataArray = [];
for (i = 0; i < fLen; i++) {
var item = pickedArray[i];
var values = valueCollection.findOne({'_id': "myDoc"}, {fields: {[item]:1 , '_id':0}});
console.log(values);
pickedDataArray.push(values);
}
console.log(pickedDataArray);
The console.log(pickedDataArray);
displays only the desired objects specified by, for instance, pickedStrings = ["A", "B"];
:
[
{ A:
{ GP: 37.56,
ES: 67.51,
EV: 12.12,
CR: 45.90,
Data: [Object]
}
},
{ B:
{ GP: 7.62,
ES: 7.51,
EV: 22.82,
CR: 75.40,
Data: [Object]
}
}
]
Now, the goal is to extract single fields like GP per object from the query (pickedDataArray
) and store them in a new array. So, we need the output:
neededArray = [37.56, 7.62]
However, there are two issues that need to be addressed. Firstly, it's unclear how to filter or retrieve just one field from this embedded object array. Attempted solutions like:
var neededArray = pickedDataArray.map(a => a.A.GP);
have resulted in arrays with undefined values. Secondly, the identifiers "A" and "B" provided by users are dynamic variables. Is it necessary to loop again using var item = pickedArray[i];
?
This process utilizes the Meteor framework with MongoDB.