Loop through Two Arrays (JS)

I have two arrays which I'm trying to loop through to determine if they have matching values. Here is a portion of the code:
currentProducts.forEach(item1 => {
pastProducts.forEach(item2 => {

console.log("Item 1: " + item1.id + " " + "Item 2: " + item2)

if (item1.id === item2) {
console.log("match")
} else {
console.log("no match")
}
})
});
currentProducts.forEach(item1 => {
pastProducts.forEach(item2 => {

console.log("Item 1: " + item1.id + " " + "Item 2: " + item2)

if (item1.id === item2) {
console.log("match")
} else {
console.log("no match")
}
})
});
When I run the comparison, it returns no match, despite there being a match (see screenshot). What am I doing wrong here?
No description
202 Replies
Matt
MattOP15mo ago
Also, what is the best way to asynchronously run this check (get result without continuing). I do have this inside of an async function, but don't know where to put an await... is it better to use for, of, instead of forEach?
ἔρως
ἔρως15mo ago
can you show the data? or some sample that reproduces the problem you have? from looking at the little code you've shown, i will say that you're comparing strings to numbers
Matt
MattOP15mo ago
That's the issue Thank you
Matt
MattOP15mo ago
No description
Matt
MattOP15mo ago
Visually it didn't seem different but it was the data type
ἔρως
ἔρως15mo ago
if i were you, i would do something different
Matt
MattOP15mo ago
What would you recommend
ἔρως
ἔρως15mo ago
i would use a set instead of an array then you can just do set.has(<value>)
Matt
MattOP15mo ago
Basically, I'm building a system that gets data from a shopify API then checks to see if theres any new products loaded to the website
ἔρως
ἔρως15mo ago
instead of double-looping
Matt
MattOP15mo ago
.has will loop through all the data in array?
ἔρως
ἔρως15mo ago
it's probably implementation-dependent but i have no idea but does it matter? the loop won't be done in javascript, but in c++ or whatever, which is going to be a lot faster
Matt
MattOP15mo ago
Do I need to create a map of the array?
ἔρως
ἔρως15mo ago
no, you can just do new Set(<array comes here>) E.g.: var pastProducts = new Set(["value1", "value2", "value3", "value4", "value1"]); and this will also automatically get rid of duplicates the annoying thing is to get the values back from it, which you have to do Array.from(set) or [...set]
Matt
MattOP15mo ago
what if the value I'm checking for is from another array
ἔρως
ἔρως15mo ago
what do you mean?
Matt
MattOP15mo ago
how do i iterate using set.has(<value>)
ἔρως
ἔρως15mo ago
you don't
Matt
MattOP15mo ago
or can <value> be an array ?
ἔρως
ἔρως15mo ago
that's the point no, you iterate over just currentProducts but if you make pastProducts into a set, you don't need the 2nd forEach
Matt
MattOP15mo ago
Well I'm going to be setting pastProducts = currentProducts after the check because after every iteration, I want to check the new set
ἔρως
ἔρως15mo ago
you can add the value to the set and next time you check, the set will have the ids you had previously
Matt
MattOP15mo ago
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = new Set();
let pastProducts = [6861976567895];


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {

currentProducts.add(data.products)

pastProducts.forEach(product => {
console.log(currentProducts.has(product))
});

})
.catch(err => console.log("error:" + err))


}

checkStock ()
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = new Set();
let pastProducts = [6861976567895];


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {

currentProducts.add(data.products)

pastProducts.forEach(product => {
console.log(currentProducts.has(product))
});

})
.catch(err => console.log("error:" + err))


}

checkStock ()
This returns false
ἔρως
ἔρως15mo ago
that's because you did the opposite the idea is for pastProducts to be the new Set
Matt
MattOP15mo ago
hmm I'm just a bit confused So even if it's true how would I do something afterwards ? use an if statement to see if pastProduct has is true?
ἔρως
ἔρως15mo ago
var pastProducts = new Set([6861976567895]);

// [...]

currentProducts.forEach(item1 => {
console.log("Item 1: " + item1.id + " " + "Item 2: " + item2)

if (pastProducts.has(item1.id)) {
console.log("match")
} else {
console.log("no match")
}
});
var pastProducts = new Set([6861976567895]);

// [...]

currentProducts.forEach(item1 => {
console.log("Item 1: " + item1.id + " " + "Item 2: " + item2)

if (pastProducts.has(item1.id)) {
console.log("match")
} else {
console.log("no match")
}
});
something like this all that looping and checking is just a single if now
Matt
MattOP15mo ago
hmm Logging as no match for me
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = [];
var pastProducts = new Set([6861976567895]);


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {

currentProducts.push(data.products)

currentProducts.forEach(item => {

if (pastProducts.has(item.id)) {
console.log("match")
} else {
console.log("no match")
}
});
})
.catch(err => console.log("error:" + err))


}

checkStock ()
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = [];
var pastProducts = new Set([6861976567895]);


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {

currentProducts.push(data.products)

currentProducts.forEach(item => {

if (pastProducts.has(item.id)) {
console.log("match")
} else {
console.log("no match")
}
});
})
.catch(err => console.log("error:" + err))


}

checkStock ()
ἔρως
ἔρως15mo ago
you're looping the wrong thing
Matt
MattOP15mo ago
item is returning object
ἔρως
ἔρως15mo ago
you're still looping the wrong thing you have to loop data.products, you're looping [data.products]
Matt
MattOP15mo ago
currentProducts is equal to data.products
ἔρως
ἔρως15mo ago
no, it isn't you pushed an array into it
Matt
MattOP15mo ago
currentProducts.push(data.products) thats an array with all the items under 1 index ([0]) so i have to loop through the [0] index?
Matt
MattOP15mo ago
No description
ἔρως
ἔρως15mo ago
this is what you're doing
No description
ἔρως
ἔρως15mo ago
you're pushing an array into the array so you have an array in the array
Matt
MattOP15mo ago
should i be iterating through the array originally so each entry is it's own index? instead of setting my empty array to an array
ἔρως
ἔρως15mo ago
you can do 2 ways: push the ones that don't exist, or concat 2 arrays
Matt
MattOP15mo ago
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = [];
let pastProducts = new Set([6861976567895]);


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {
let products = data.products
products.forEach(item => {
currentProducts += item;
console.log("item:" + item)
if (pastProducts.has(item.id)) {
console.log("match")
} else {
console.log("no match")
}
});
})
.catch(err => console.log("error:" + err))


}

checkStock ()
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = [];
let pastProducts = new Set([6861976567895]);


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {
let products = data.products
products.forEach(item => {
currentProducts += item;
console.log("item:" + item)
if (pastProducts.has(item.id)) {
console.log("match")
} else {
console.log("no match")
}
});
})
.catch(err => console.log("error:" + err))


}

checkStock ()
This works
ἔρως
ἔρως15mo ago
yeah, because you aren't looping anything besides the products those are different things
Matt
MattOP15mo ago
im hella confused now lol When I fetch the URL it returns JSON with 30 indexed items I need to store each of these items into an array (currentProducts), which is why I looped through and added
ἔρως
ἔρως15mo ago
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = [];
var pastProducts = new Set([6861976567895]);


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {
data.products.forEach(item => {

if (pastProducts.has(item.id)) {
console.log("match");
return;
}

console.log("no match");

pastProducts.add(item.id);
currentProducts.push(item);
});
})
.catch(err => console.log("error:" + err))


}

checkStock ()
const url = "https://www.hanon-shop.com/collections/whats-new/products.json";

let currentProducts = [];
var pastProducts = new Set([6861976567895]);


async function checkStock () {


fetch(url)
.then(response => response.json())
.then(data => {
data.products.forEach(item => {

if (pastProducts.has(item.id)) {
console.log("match");
return;
}

console.log("no match");

pastProducts.add(item.id);
currentProducts.push(item);
});
})
.catch(err => console.log("error:" + err))


}

checkStock ()
this is how i would do it but better formatted forgot 1 line, added it now pastProducts will have all the ids
Matt
MattOP15mo ago
won't this call regardless? console.log("no match");
ἔρως
ἔρως15mo ago
if you need to find by id, you can use a map instead of the array no, because of the return this is called a "guard clause" or "early return"
Matt
MattOP15mo ago
the return will skip pushing the product to array though, no?
ἔρως
ἔρως15mo ago
yes
Matt
MattOP15mo ago
i need the products in array so maybe ill get rid of the return for now
ἔρως
ἔρως15mo ago
the return is essential unless you want to add past products too
Matt
MattOP15mo ago
I need this data stored because this script will run continuously and check every X min after the check, ill be setting pastProducts to currentProducts
ἔρως
ἔρως15mo ago
so, if you are going to put all the data into the array, regardless of having seen it or not, why are you checking it?
Matt
MattOP15mo ago
HANON
What's New
Never miss a trick. Check out our newest styles and limited-edition sneakers, sourced from the secret corners of the streetwear world.
Matt
MattOP15mo ago
will randomly add new products to their website throughout the day my goal is to be able to determine when a new product is added, by checking the JSON every X min and comparing to previous interval
ἔρως
ἔρως15mo ago
that's quite different from what you're shown
Matt
MattOP15mo ago
Well, the pastProduct value was set to a fixed number just to test the logic once i finish the logic for it, ill have to run it for a few hours to see if it actually works. It's just very basic rn but i have to add the setInterval, etc thats why im confused with the .has because pastProducts will also be an array, so ill be comparing an array with an array. From my understanding, .has is only used for a fixed value
ἔρως
ἔρως15mo ago
if you remove the return, you can set a variable to a value that lets you know that new products were added but that will wait for tomorrow, because i should be sleeping 2 hours ago
Matt
MattOP15mo ago
haha thank you for the input Hit the hay
ἔρως
ἔρως15mo ago
lol by the way, everything there can be made into a map and it would be way easier to do everything you need, except maybe looping, but that isnt too hard
Matt
MattOP15mo ago
Okay Thank you @ἔρως I managed to do this by creating a function with a for loop
ἔρως
ἔρως15mo ago
🤮 but at long as it works...
Matt
MattOP15mo ago
const url = "https://www.atmosusa.com/collections/new-arrivals/products";

let products = new Set();

fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

let currentProducts = new Set(data.products);
const result = checkStock(products, currentProducts);

result.forEach((item) => {
console.log("New Product Detected: " + item);
});

products.clear();
})
.catch((err) => console.log("error:" + err));

function checkStock(oldProd, newProd) {
let duplicateProd = [];

if (products != null) {
for (let i of newProd) {
if (!oldProd.has(i.id)) {
duplicateProd.push(url + i.handle);
}
}
} else {
products = oldProd;
}
return duplicateProd;
}
const url = "https://www.atmosusa.com/collections/new-arrivals/products";

let products = new Set();

fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

let currentProducts = new Set(data.products);
const result = checkStock(products, currentProducts);

result.forEach((item) => {
console.log("New Product Detected: " + item);
});

products.clear();
})
.catch((err) => console.log("error:" + err));

function checkStock(oldProd, newProd) {
let duplicateProd = [];

if (products != null) {
for (let i of newProd) {
if (!oldProd.has(i.id)) {
duplicateProd.push(url + i.handle);
}
}
} else {
products = oldProd;
}
return duplicateProd;
}
Bad code? Rn this returns all the array elements just to test the logic
ἔρως
ἔρως15mo ago
where's the old products?
Matt
MattOP15mo ago
I pass products to oldProd
ἔρως
ἔρως15mo ago
i will check this later
Matt
MattOP15mo ago
Let me know your thoughts, I’d like to improve wherever possible 👍🏻 Thanks
ἔρως
ἔρως15mo ago
i have a few ideas
Matt
MattOP15mo ago
👀
ἔρως
ἔρως15mo ago
so, what exactly do you want? just so i can re-check your requirements and then go from there
Matt
MattOP15mo ago
I call that API and receive JSON I then need to parse the JSON and store the ID (currentProducts). Then I check to see if products is not empty. If it’s not, I set currentProducts to products. Then check to see if currentProducts contains any id’s differing from the previous iteration. If there is a change, I then create a URL and output to signify there’s a new product loaded TLDR: Scrape URL (consistently) and output when new products are loaded. My current solution is a single iteration but I will be adding setInterval to consistently check @ἔρως https://www.atmosusa.com/collections/new-arrivals/products.json returns the newest 30 products
ἔρως
ἔρως15mo ago
you're describing what you want the code to look like not what you want to do
Matt
MattOP15mo ago
Tldr ^
ἔρως
ἔρως15mo ago
which features do you want the code to have? just those?
Matt
MattOP15mo ago
We’ll I plan to add more This was the starting point
ἔρως
ἔρως15mo ago
then can you explain just the plan for the data processing, and part of the data rendering?
Matt
MattOP15mo ago
I’m confused
ἔρως
ἔρως15mo ago
well, what do you intend to do with the data?
Matt
MattOP15mo ago
Why Like how I plan to store it?
ἔρως
ἔρως15mo ago
no, what do you plan to do with the data im trying to understand the whole picture, not just the code
Matt
MattOP15mo ago
To log when new items arrive on page Just practice code I’m trying to optimize
ἔρως
ἔρως15mo ago
will to store the information somewhere?
Matt
MattOP15mo ago
Maybe in the future, idk You mean like a DB?
ἔρως
ἔρως15mo ago
no, stored localy in the browser (like a session storage or local storage or something else)
Matt
MattOP15mo ago
No won’t be doing that This is just a practice solution I want to store it locally eventually, maybe on a text file using FS with Node, or a DB, to be able to practice
ἔρως
ἔρως15mo ago
so, for now, just assume new data from the beginning?
Matt
MattOP15mo ago
Huh? Just want to pull from API and check with previous iteration
ἔρως
ἔρως15mo ago
i know that im sure there's a much better way to do this, but will have to be for tomorrow
Matt
MattOP15mo ago
Ah okay Lmk if you have any brief ideas which I can research My code works but I’d like to do what’s Optimal
ἔρως
ἔρως15mo ago
what's optimal sometimes isnt the best im just trying to find a better solution than loops in loops or a function in a loop or something nasty like that
Matt
MattOP15mo ago
I found some different array methods to check for matches, but they just return a Boolean value And I can’t seem to find any other solutions to compare two arrays besides loops
ἔρως
ἔρως15mo ago
yeah, which is why i know there are better solutions for example, i cant see why a map wont work for you
Matt
MattOP15mo ago
I’ll keep researching to see if I can find something
ἔρως
ἔρως15mo ago
but i may be missing something thats why i ask about intent, plans, prospects and all that, to be sure if something works or not
Matt
MattOP15mo ago
I tried to use .map with a for loop then compare but it wasn’t working I don’t have the code atm on mobile Makes sense
ἔρως
ἔρως15mo ago
no no, not .map a map
Matt
MattOP15mo ago
The logic may change depending on what I do with it
ἔρως
ἔρως15mo ago
its like a set, but you use a key to map to a value
Matt
MattOP15mo ago
Map - JavaScript | MDN
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
ἔρως
ἔρως15mo ago
yes
Matt
MattOP15mo ago
I’m going to read about this now to try and figure it out. Maybe this’ll work, I originally tried the array method .map
ἔρως
ἔρως15mo ago
that method is almost like a foreach, but takes the returned value and puts it in a new array [1, 2, 3].map(x=>x*5) returnd [5, 10, 15]
Matt
MattOP15mo ago
Ahh, makes sense. That’s why I wasn’t able to directly compare because it returns array
ἔρως
ἔρως15mo ago
no, its because you cant compare arrays, since you are just checking if the object instance is the same or not but the way i see this, is just a "shove everything into the map, save the ids of the recently obtained elements and render from there"
Matt
MattOP15mo ago
How would you iterate through the map then? To determine similarities For of?
ἔρως
ἔρως15mo ago
why would you need to check for "similarities"? what is a "similarity" anyway?
Matt
MattOP15mo ago
What solution would you recommend
const url = "https://www.atmosusa.com/collections/new-arrivals/products";

let oldIds = new Array();

fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

// Create an Array by grabbing products and storing their ID
let newIds = data.products.map((item) => item.id)
oldIds = newIds

newIds.forEach(id => {
if (oldIds.includes(id)) {
console.log("true")
} else {
console.log("false")
}
});

console.log(oldIds + newIds)

})
.catch((err) => console.log("Error:" + err));
const url = "https://www.atmosusa.com/collections/new-arrivals/products";

let oldIds = new Array();

fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

// Create an Array by grabbing products and storing their ID
let newIds = data.products.map((item) => item.id)
oldIds = newIds

newIds.forEach(id => {
if (oldIds.includes(id)) {
console.log("true")
} else {
console.log("false")
}
});

console.log(oldIds + newIds)

})
.catch((err) => console.log("Error:" + err));
I tried something like this but can't seem to get .includes method to work
ἔρως
ἔρως15mo ago
i wouldnt use .map since it is the wrong tool for the job you can nail a screw with the handle of a screwdriver, but just because it worked doesnt mean that that's the right way to use those
Matt
MattOP15mo ago
what would you use ?
ἔρως
ἔρως15mo ago
a single map not .map, but a map
Matt
MattOP15mo ago
and how would you loop to find duplicates with two maps?
ἔρως
ἔρως15mo ago
1 map you can check if it has the id but im not sure if it is the best solution
Matt
MattOP15mo ago
So similar to my example Just with a map not map method This
ἔρως
ἔρως15mo ago
no, i would use a map and a set or maybe 2 maps actually, just 1 map and 1 set the set would have the ids of the new items and the map would have everything you need to render those
Matt
MattOP15mo ago
And how would you iterate between them to determine matches I’m about to just redo this concept to determine based on updated time in the JSON instead of comparing IDs
ἔρως
ἔρως15mo ago
you dont that's the point of the map you check if the map has the key, and if it doesnt, you add the object to the map and an id to the set the set will have all the new ids to render you loop through the set and get all the new ids
Matt
MattOP15mo ago
What’s the difference from using .has with map or .includes with array ?
ἔρως
ἔρως15mo ago
speed also, the set only takes unique values and you can empty it super quickly
Matt
MattOP15mo ago
Okay good to know
ἔρως
ἔρως15mo ago
with an array, to empty it, you have to do weird stuff like setting the lengh to 0 but the set has the .clear method
Matt
MattOP15mo ago
I’ll see if I can spin something up with that suggestion in about 30 min You can’t just set array back to []?
ἔρως
ἔρως15mo ago
you can, but that doesnt empty the array that's a new array new array means more memory allocations, and it may not free the old values for a bit, until the garbage collector runs
Matt
MattOP15mo ago
Is the old array stored somewhere?
ἔρως
ἔρως15mo ago
not usually, no
Matt
MattOP15mo ago
Ahh okay didn’t know that Also good to know, thanks
ἔρως
ἔρως15mo ago
this is the minor concert to pick a set instead of an array
Matt
MattOP15mo ago
@ἔρως I'm reading about Map now and am a bit confused about .get() method
const map1 = new Map();
map1.set('bar', 'foo');

console.log(map1.get('bar'));
// Expected output: "foo"
const map1 = new Map();
map1.set('bar', 'foo');

console.log(map1.get('bar'));
// Expected output: "foo"
Why does console.log(map1.get('bar')); return foo, seems like it should return bar? I don't quiet understand the mozilla explanation for this
ἔρως
ἔρως15mo ago
bar is the key foo is the value its a key-value pair
Matt
MattOP15mo ago
ahh
Matt
MattOP15mo ago
No description
Matt
MattOP15mo ago
Makes sense why there's undefined associated with each pair
ἔρως
ἔρως15mo ago
store the objet as the value, and its done
Matt
MattOP15mo ago
So I need to set the key (as index) then value as id?
ἔρως
ἔρως15mo ago
no, value as the entire object
Matt
MattOP15mo ago
all the IDs under one pair? id => [all ids]
ἔρως
ἔρως15mo ago
no isnt it 1 big object per id?
Matt
MattOP15mo ago
No description
Matt
MattOP15mo ago
each index returned is an object
ἔρως
ἔρως15mo ago
yes, and you store all that stuff
Matt
MattOP15mo ago
Okay and as the key, should I do something similar to array index? Map(1, object) Map(2, object) etc? Run a for loop, set the index to key and value to index.value (returned array)?
ἔρως
ἔρως15mo ago
no, you use the .get method to obtain a value
Matt
MattOP15mo ago
No description
Matt
MattOP15mo ago
To set value
let newProducts = new Map();

data.products.forEach(function (value, index) {
newProducts.set(value,index)
});
let newProducts = new Map();

data.products.forEach(function (value, index) {
newProducts.set(value,index)
});
ἔρως
ἔρως15mo ago
yup that's it
Matt
MattOP15mo ago
Ah this is much cleaner I was looking to store the object data, I was going to in separate arrays
ἔρως
ἔρως15mo ago
now you have everything but the downside is that it doesn't tell you which ids are new
Matt
MattOP15mo ago
🥹
ἔρως
ἔρως15mo ago
that is why you create a set and then you check if the map has the id if it doesnt, then add the id to the set that's it
Matt
MattOP15mo ago
This is very frustrating lol
ἔρως
ἔρως15mo ago
i think it is way easier than 2 loops
Matt
MattOP15mo ago
I need to do two loops still A second loop to iterate and check for duplicates Just can’t seem to get this damn has method to work lol
ἔρως
ἔρως15mo ago
that's the magic trick maps dont have duplicates you cant map multiple values to a key
Matt
MattOP15mo ago
I need to compare it to the other map to see if there’s any changes
ἔρως
ἔρως15mo ago
then do this: DONT get the value from the map
Matt
MattOP15mo ago
Lol And compare it to ?
ἔρως
ἔρως15mo ago
you cant compare objects
Matt
MattOP15mo ago
I’m trying to compare the object ids
ἔρως
ἔρως15mo ago
so, you need to find something else why?
Matt
MattOP15mo ago
Man I’m trying to see if there’s any changes
ἔρως
ἔρως15mo ago
the object id will always be the same
Matt
MattOP15mo ago
No I’m trying to make the products To see if there’s any new ids added
ἔρως
ἔρως15mo ago
then do what i said
Matt
MattOP15mo ago
If the id is new then report it I’m trying to but I have to loop through to check still
ἔρως
ἔρως15mo ago
no, you dont give me 90 minutes and i will make it work
Matt
MattOP15mo ago
My heads gonna explode Ima step away for a few min getting frustrated with map😅
ἔρως
ἔρως15mo ago
if it doesnt work its because you are doing something you shouldnt but, i will help you after
Matt
MattOP15mo ago
What's the difference betwen Set & Map? Seems like they have similar methods and can set key to value (not same terminology, but something like mySet1.add({ a: 1, b: 2 }); )
const url = "https://www.atmosusa.com/collections/new-arrivals/products";
let products = new Set();

function checkStock () {
fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

let newProducts = new Map();

data.products.forEach(function (value, i) {
products.set(i, value)
});

for (const [key, value] of newProducts.entries()) {
if(!products.has(value.id)) {
products.add(value.id)
} else {
console.log("Set does contain: " + key + value)
}
}

})
.catch((err) => console.log("Error:" + err));

}
const url = "https://www.atmosusa.com/collections/new-arrivals/products";
let products = new Set();

function checkStock () {
fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

let newProducts = new Map();

data.products.forEach(function (value, i) {
products.set(i, value)
});

for (const [key, value] of newProducts.entries()) {
if(!products.has(value.id)) {
products.add(value.id)
} else {
console.log("Set does contain: " + key + value)
}
}

})
.catch((err) => console.log("Error:" + err));

}
ἔρως
ἔρως15mo ago
well, the name is self-explanatory to me a set is a set of unique values a map maps an unique key to a single value
Matt
MattOP15mo ago
is this the kind of logic you were thinking?
ἔρως
ἔρως15mo ago
no, but its close
Matt
MattOP15mo ago
😅 This logic seems to work but still contains two for loops
ἔρως
ἔρως15mo ago
your loop works by miracle, if at all
Matt
MattOP15mo ago
lol Why do you say this?
ἔρως
ἔρως15mo ago
you created an empty map, then you are looping it
Matt
MattOP15mo ago
data.products.forEach(function (value, i) {
products.set(i, value)
});
data.products.forEach(function (value, i) {
products.set(i, value)
});
sets the key and value to the map though, no?
ἔρως
ἔρως15mo ago
you are setting into products but you loop newProducts
Matt
MattOP15mo ago
const url = "https://www.atmosusa.com/collections/new-arrivals/products";
let productID = new Set();

function checkStock () {
fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

let newProducts = new Map();

data.products.forEach(function (value, i) {
newProducts.set(i, value)
});


for (const [key, value] of newProducts.entries()) {
if(!productID.has(value.id)) {
console.log("New Product Detected: " + value.id)
productID.add(value.id)
} else {
console.log("No new products")
return
}
}

})
.catch((err) => console.log("Error:" + err));

}
const url = "https://www.atmosusa.com/collections/new-arrivals/products";
let productID = new Set();

function checkStock () {
fetch(url + ".json")
.then((response) => response.json())
.then((data) => {

let newProducts = new Map();

data.products.forEach(function (value, i) {
newProducts.set(i, value)
});


for (const [key, value] of newProducts.entries()) {
if(!productID.has(value.id)) {
console.log("New Product Detected: " + value.id)
productID.add(value.id)
} else {
console.log("No new products")
return
}
}

})
.catch((err) => console.log("Error:" + err));

}
sorry, there was typo
ἔρως
ἔρως15mo ago
your 2 loops should be 1
Matt
MattOP15mo ago
is there a method similar to .map i can use to set map values besides looping
ἔρως
ἔρως15mo ago
wait a bit
const url = "https://www.atmosusa.com/collections/new-arrivals/products";
let products = new Map();
let newProducts = new Set();
let updatedProducts = new Set();

function checkStock () {
fetch(url + ".json")
.then((response) => response.json())
.then((data) => {
// Clean up old data! We starting fresh.
newProducts.clear();
updatedProducts.clear();

data.products.forEach(function(product) {
// We need to check if the product already exists, to check updates
if(products.has(product.id)) {
var oldProduct = products.get(product.id);

// If there are no updates, leave the forEach
if(product.updated_at === oldProduct.updated_at) {
return;
}

// Otherwise, we store it for future reference
updatedProducts.add(product.id);
} else {
// If we are here, the data is new
newProducts.add(product.id);
console.log("New Product Detected: " + product.id);
}

products.set(product.id, product);
});

if(!newProducts.size) {
console.log("No new products")
}
})
.catch((err) => console.log("Error:" + err));

}
const url = "https://www.atmosusa.com/collections/new-arrivals/products";
let products = new Map();
let newProducts = new Set();
let updatedProducts = new Set();

function checkStock () {
fetch(url + ".json")
.then((response) => response.json())
.then((data) => {
// Clean up old data! We starting fresh.
newProducts.clear();
updatedProducts.clear();

data.products.forEach(function(product) {
// We need to check if the product already exists, to check updates
if(products.has(product.id)) {
var oldProduct = products.get(product.id);

// If there are no updates, leave the forEach
if(product.updated_at === oldProduct.updated_at) {
return;
}

// Otherwise, we store it for future reference
updatedProducts.add(product.id);
} else {
// If we are here, the data is new
newProducts.add(product.id);
console.log("New Product Detected: " + product.id);
}

products.set(product.id, product);
});

if(!newProducts.size) {
console.log("No new products")
}
})
.catch((err) => console.log("Error:" + err));

}
Matt
MattOP15mo ago
That looks good, studying it over I appreciate it
ἔρως
ἔρως15mo ago
you're welcome
Matt
MattOP15mo ago
@ἔρως It works 👍
Matt
MattOP15mo ago
No description
ἔρως
ἔρως15mo ago
nice by the way, i didn't test it at all
Matt
MattOP15mo ago
Most Shopify stores use similar url for their new products I have a working solution already which I compared it to so I knew what site to test it on to see if it would pick up I appreciate you helping with most my questions though. I’m still new to this but I’m learning a lot from these practical sample codes 👍🏻
ἔρως
ἔρως15mo ago
that's how you learn get your hands dirty do something
Matt
MattOP15mo ago
Say for example if I wanted to run this across multiple websites That would require node.js & multi threading?
ἔρως
ἔρως15mo ago
nodejs? probably multi threading? i dont think so
Matt
MattOP15mo ago
Wouldn’t I need to run the script (per site) separately?
ἔρως
ἔρως15mo ago
fetch is asyncronous, so, probably not
Matt
MattOP15mo ago
All the products would be stored under the same variables and wouldn’t be differentiated between sites
ἔρως
ἔρως15mo ago
are all shoppify ids unique between websites?
Matt
MattOP15mo ago
I believe so
ἔρως
ἔρως15mo ago
so, if they are all unique (as in, the id is provided by shoppify itself, not by the instance), then you don't need to worry
Matt
MattOP15mo ago
Yeah but you’d need to be able to determine which new add came from where Seems like a complicated mess
ἔρως
ἔρως15mo ago
everything has an unique link
Matt
MattOP15mo ago
I’m thinking it would need to be multi thread Because if this was scaled and delay times were lowered, Shopify would rate limit fast If it was multi thread each could have its own IP which could prevent rare limiting from requests Like for example if you were to run with 100 sites, it probably wouldn’t run at all considering the amount of request
ἔρως
ἔρως15mo ago
that's not multi-threading that's multiple processes
Matt
MattOP15mo ago
Yeah that’s what I mean Where you can use this file as a function basically and call it per URL or something I might not be using the right terminology
ἔρως
ἔρως15mo ago
if you're worried about rate limits, yeah
Matt
MattOP15mo ago
No description
Matt
MattOP15mo ago
If I wanted to add more data, such as variant sizing and availability, would it be bad to do a nested For Loop?
ἔρως
ἔρως15mo ago
no, you need it
Want results from more Discord servers?
Add your server