1. /**
  2. * Filters data in lists for isolated environments, using default values.
  3. *
  4. * @param {Array<Array<any>>} data - The data to filter. Each inner array represents a list.
  5. * @param {object} defaults - An object containing default values for each field.
  6. * @returns {Array<Array<any>>} - The filtered data with default values applied.
  7. */
  8. function filterForIsolatedEnvironments(data, defaults) {
  9. if (!Array.isArray(data)) {
  10. return []; // Handle invalid input gracefully
  11. }
  12. const filteredData = [];
  13. for (const list of data) {
  14. if (!Array.isArray(list)) {
  15. filteredData.push([]); //Handle invalid list entries
  16. continue;
  17. }
  18. const filteredList = [];
  19. for (const item of list) {
  20. if (typeof item === 'object' && item !== null) {
  21. const newItem = { ...item }; // Create a copy to avoid modifying the original
  22. for (const key in defaults) {
  23. if (!(key in newItem)) {
  24. newItem[key] = defaults[key]; // Apply default value if key is missing
  25. }
  26. }
  27. filteredList.push(newItem);
  28. } else {
  29. filteredList.push(item); //Keep non-object items as they are.
  30. }
  31. }
  32. filteredData.push(filteredList);
  33. }
  34. return filteredData;
  35. }

Add your comment