Reputation: 3586
I'm trying to cache some informations in a node CLI script using an array or an object. I want to have a simple key value that I can ckeck to get usernames using the id that I want to use as the key. I'm trying with this code but I'm not sure if it will work as expected
let threads = [];
ig.realtime.on('message', async (data) => {
if( String(data.message.user_id) === ig.state.cookieUserId ){
return;
} else if ( data.message.path.endsWith('has_seen') ){
return;
} else if( !threads.includes(data.message.thread_id) ){
const thread = await ig.feed.directThread({thread_id: data.message.thread_id}).request();
threads[thread.thread.thread_id] = {
username: thread.thread.users[0].full_name,
}
}
});
Will the threads[thread.thread.thread_id]
add the id as key each time that it will not be found into the array or I need to use Array.push()
function?
Can I access to the key of the array in this way threads[data.message.thread_id]
to get the related value?
Upvotes: 0
Views: 1241
Reputation: 1555
use
if(!threads.find(o=>o.id==id)){
threads.push({id, ...})
}
or simply use Map() as bellow
let threads = new Map();
if(!threads.has(id)){
threads.set(id, value)
}
Upvotes: 1