How do you use AI in your JS in FM work?
I am starting to use ChatGBT so much more these days when it comes to a specific problem I have when working with JS. I don’t ask it to build a widget for me. Instead I ask it to help me organize data or manipulate the DOM in some way.
I’m curious: how do you use ChatGBT (or other services) in your work? And what’s the endgame for the result?
Here’s my example:
Yesterday I asked ChatGBT this question:
given an object like this: { "product1": {"sold": 45, "unsold" : 2}, "product2" : {"sold":11, "unsold": 22} where there are many products as properties, I need to find the top five products that were sold. use JS to do this
It immediately spat out the answer:
Certainly! You can achieve this in JavaScript by first converting the object into an array of objects, sorting it based on the "sold" quantity in descending order, and then extracting the top five products. Here's an example:
const data = {
"product1": { "sold": 45, "unsold": 2 },
"product2": { "sold": 11, "unsold": 22 },
// ... (other products)
};
// Convert object into an array of objects
const productsArray = Object.keys(data).map(key => ({ name: key, ...data[key] }));
// Sort products based on 'sold' quantity in descending order
productsArray.sort((a, b) => b.sold - a.sold);
// Get top five products that were sold
const topFiveSoldProducts = productsArray.slice(0, 5);
console.log(topFiveSoldProducts);I appreciated this answer. Even more I appreciated the explanation:
This code snippet converts the given object into an array of objects, sorts them based on the "sold" quantity, and then extracts the top five products based on their sold quantity. Adjust the object structure or add more products to data as needed.
I had to do some work to get it into my widget, but that was pretty straightforward.
After I determined it working in the JS, I took a few moments to learn the concept behind this answer. As I contemplated the code, it really was clear to me. I could see what each line was doing and could picture the step result in my head.
My Endgame
I realized what the answer was doing and can now apply this idea anywhere I need to do the same kind of thing. I can find the top 5 or 10 or 100 items in an object in this manner.
Question:
What do you do when you get the answer to a specific JS in FM problem in ChatGBT?