profile

Welcome To Slater!

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

Published about 2 months agoย โ€ขย 2 min read

๐Ÿ‘‹,

โ€‹

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!

Sign up for early access and weekly resources in your inbox

Slater resources, updates and community activity

Read more from Welcome To Slater!

You can now use OpenAI's latest and most powerful language model, GPT 4o, with Slater AI. We've been testing GPT 4o this week and Slater AI's capabilities are better and faster than ever! Along with the new model, we implemented several improvements to Slater AI. Slater AI will now give more Slater and Webflow relevant answers since we give Slater AI contextual understanding (h/t tdlabs.ca). We also improved the messages parsing (h/t @jeffamcavoy). Slater Sessions Slater Sessions are back! In...

1 day agoย โ€ขย 2 min read

We were excited to be invited back on an official Webflow livestream this week. This time, Slater played a supporting role on "Learn GSAP for Webflow." Aron and Cassie did an incredible job building a really cool GSAP animation. Aron started the stream by publishing JavaScript in Webflow, so you could see the advantages of cmd+s over the publish flow. Those few seconds it takes to publish your Webflow project feel like eternity when you've just forgotten to add a comma. ;) If you didn't see...

9 days agoย โ€ขย 1 min read

More and more Slater-powered Webflow projects are running in production! So, let's talk about what that means. Slater works a little differently in a production environment. Instead of serving a file from the Slater server, we serve a file from Amazon S3. The S3 file is extremely resilient, and by default, it is cached by the browser. When your site doesn't seem to be updated, even after publishing changes, it's likely a caching issue. Your browser is simply not getting the latest copy of the...

16 days agoย โ€ขย 2 min read
Share this post