In: Computer Science
[Javascript] Create a function(returnObjectFromId(case, ...idNum)) to return the case Object(s) for a given idNum, or list of idNums.
Calling with a single `idNum` value should return the case Object, and return NULL if an id value that's unknown is passed
returnObjectFromId(case, 84838) would return the Object in the cases Array with
an 'idNumber' of id, and use the .find() method of the cases Array to locate items by idNumber.
returnObjectFromId(cases, -23298312) would return null.
returnObjectFromId(cases, 161020, 161021) would return an Array of case Objects
with two elements, matching the id values. We don't add anything to the returned Array,
if any of the ids in the list are unknown.
As an example, the following function would return an Array of 2 case Objects, ignoring the unknown
id(-23298312):
returnObjectFromId(cases, 231321, 241249, -23298312) would return an Array of 2 cases.
example of case object:
case = {
"idNumber": 112319,
"Reported Date": "2020-08-15",
"Episode Date": "2020-07-12",
},
{
"idNumber": 132421,
"Reported Date": "2020-08-12",
"Episode Date": "2020-07-19",
},
...etc
Hello,
I have created returnObjectFromId function based on your requirement. I have used sample data as provided by you in question. Tested the code with valid and invalid inputs. Attached returnObjectFromId function and output screenshots. Please let me know if any changes required.
returnObjectFromId function:
// Function to return objects which matches id in the cases list
function returnObjectFromId(cases, ...idNum) {
// Declared an empty array object to store output
var resultIdArr = [];
// Iterating through the idNum array
idNum.forEach(idNumber => {
// Identify case with idNumber by using find method in array prototype
const caseFound = cases.find(element => element.idNumber === idNumber);
// If case found with idNumber then add it to result array
if(caseFound) {
resultIdArr.push(caseFound);
}
});
// If result array length is equal to 0 that return null means there are no case found with given list of id numbers
if(resultIdArr.length == 0) {
return null;
} else {
// Else return result array
return resultIdArr;
}
}
Test with valid input:
// Sample cases data based data available in question
var casesData = [{
"idNumber": 112319,
"Reported Date": "2020-08-15",
"Episode Date": "2020-07-12",
},
{
"idNumber": 132421,
"Reported Date": "2020-08-12",
"Episode Date": "2020-07-19",
}];
// Method call returnObjectFromId function with cases data and valid and invalid id numbers
var result = returnObjectFromId(casesData, 112318, 112319, 132421);
// Printing the result
console.log(result);
Output when valid input:
Test with invalid input:
// Sample cases data based data available in question
var casesData = [{
"idNumber": 112319,
"Reported Date": "2020-08-15",
"Episode Date": "2020-07-12",
},
{
"idNumber": 132421,
"Reported Date": "2020-08-12",
"Episode Date": "2020-07-19",
}];
// Method call returnObjectFromId function with cases data and invalid id numbers
var resultNull = returnObjectFromId(casesData, 112318, -112319, -132421);
// Printing the result
console.log(resultNull);
Output when invalid input: