Do you know how to create an array of objects? ๐Ÿค”


๐Ÿ‘‹,

โ€‹

We are big fans of Ran and the giant role he plays in the #nocode space. Pleased to see that he uses Slater!

Have you tried out his courses?


Are you using the Library tab?

What code snippets are you saving?

Would you like any improvements?


Javascript 101: Arrays of Objects

We had issues with Finsweet's CMS Load so this week we've been considering implementing our own load more solution. To do this, we need to create an array of objects. Do you know what an array of objects is? Do you know how to create one? Let's discuss.

To start, an object in JavaScript is like a container that holds different pieces of information, called properties. For example, you can have an object representing a person, with properties like name, age, and city.

An array is like a list of items. Instead of just one object, you can have many objects stored in an array.

Now, let's say you have a bunch of HTML elements on a web page, like cards or list items, and you want to create an array of objects from them. This way, you can easily access and manipulate the data from those elements using JavaScript.

Here's an example of how you could do that:

```

// Select all the HTML elements you want to create objects from

const htmlElements = document.querySelectorAll('.card');

โ€‹

// Create an empty array to store the objects

const cards = [];

โ€‹

// Loop through each HTML element

htmlElements.forEach((element) => {

// Create an object for this element

const card = {

title: element.querySelector('h2').textContent,

description: element.querySelector('p').textContent,

imageUrl: element.querySelector('img').src

};

// Add the object to the cards array

cards.push(card);

});

โ€‹

// Now you have an array of card objects!

console.log(cards);

```

In this example, we're selecting all the HTML elements with the class 'card'. Then, we create an empty array called cards.

Next, we loop through each HTML element using forEach. For each element, we create an object with properties like title, description, and imageUrl. We get the values for these properties from the HTML element itself (e.g., the text content of an h2 tag, or the src attribute of an img tag).

Finally, we add (or "push") this object to the cards array.

After the loop finishes, we have an array called cards that contains all the card objects we created from the HTML elements. We can then use this array of objects to manipulate the cards on the page. For example, if the user were to select a value from a filter, we could show only those cards that relate to the filter selection.

Do you see how we can use an array of objects to get similar functionality as CMS Load?

Happy coding!

โ€‹

๐Ÿค™ The Slater team


โ€‹

๐Ÿ™ If Slater helps you create better websites, please support the team behind it.

โ€‹
โ€‹
1295 Canyon View Rd, Midway, UT 84049
โ€‹Unsubscribe ยท Preferencesโ€‹

โ€‹

Welcome To Slater!

Slater resources, updates and community activity

Read more from Welcome To Slater!

Are you using Webflow localization? We've received several requests to provide better support for it, so we're working on an update. I just finished writing the code. We need to do some testing before releasing the update early next week. Community library scripts โฏ๏ธ Pause/Reset Video Embed on Click Stops all iframe videos by resetting their source on '.close-video' button click. From Corey Moen. โฐ Progress Bar TimerJavaScript code to dynamically update a progress bar based on time between...

The AI world moves fast, and we are keeping up. This week, OpenAI released a new model, GPT 4.o mini. GPT 4.0 mini is an improved, cost-effective model that we are supporting under Slater's free tier. The default Slater AI will continue to be powered by GPT 3.5 as we test GPT 4.0 mini, but you can configure GPT 4.0 mini or even GPT 4.0 (paid) today. Community library scripts ๐Ÿ“ Fix Paragraph Runts With Nonbreaking SpacePrevent single-word runts in HTML elements with the class `.no-runt`. From...

We're back from a company-wide summer pause (or winter pause for our Southern Hemisphere team members). Edgar Allan is hitting it's numbers! Let's look at some new Community Library scripts: ๐Ÿ‘€ Dynamically loads HTML content based on a random number. This is a quick script we wrote to give a dynamic intro into a soon-to-be-released https://edgarallan.com. https://slater.app/community_library/random-content-aee7f7f2-3ab8-4e6f-887e-ea54cad0ebbf From Jared Malan โœ๏ธ Create a variable for your...