assign a name to an array of arrays

I’ve made a bit of an overhaul, but here is another option:

$(async function() {
  const data = await $.get("https://spreadsheets.google.com/feeds/list/1VC633BXpMElJjRWvIRuZIP7UrEhuw6BdscnrV2heox0/1/public/full?alt=json");
  
  const arrays = {};
  for (const entry of data.feed.entry) {
    for (const key in entry) {
      if (key.slice(0, 4) != "gsx$") continue;
      if (!entry[key].$t.trim()) continue;

      const name = key.slice(4); // optional, you could also use `key`
      if (!arrays[name]) arrays[name] = [];
      arrays[name].push(entry[key].$t);
    }
  }
  
  console.log(arrays);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

This solution first creates an object arrays which will hold the different arrays. I then loop through both the data.feed.entry values and for each entry I will loop through the keys. If the key does not start with "gsx$" or if the associated value is an empty string after trimming I will skip to the next iteration.

If we have not skipped, remove the first 4 characters from key and assign this new value to name (optional). Then check if arrays contains the property name, if not assign the arrays[name] to an empty array.

Lastly push the value into the array.

I tried to make this answer understandable, if you have any question don’t be afraid to comment.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top