This is my object
[0 … 200]
0: {id: 2291798516830927, url: '.....', address: 'new', symbol: '1t43', fav: false}
1: {id: 7337710716729779, url: '.....', address: 'new', symbol: 'gf23525', fav: false}
2: {id: 6962996031953837, url: '.....', address: 'old', symbol: '435234', fav: false}
3: {id: 8456218226475281, url: '.....', address: 'old', symbol: '2345', fav: false}
4: {id: 6759748921116029, url: '.....', address: 'new', symbol: '$B2345ROKE', fav: false}
...
address="new"
bookmarks.map(function (bookmark, index) {
setTimeout(() => {
if (bookmark.address == address) {
console.log(bookmark.symbol)
}
}, 1000 * index)
})
What I want to do is skip all the "old" addresses in my object and continue with the mapping. There should be a 2 second timeout if possible between each cycle.
if I use Timeout with value "1000 * index
" after the first two objects I have to wait several minutes before the cycle resumes, it is not clear to me why.
if I use the timeout without with "1000
" without "index
" the timeout does not work, the object is printed entirely without timeout
CodePudding user response:
I would filter and not use map. Never use a map when you mean forEach and do not need the array map is creating.
We can use setTimeout or setInterval and clear the inteval when cnt === list.length
const bookmarks = [ {id: 2291798516830927, url: '.....', address: 'new', symbol: '1t43', fav: false},
{id: 7337710716729779, url: '.....', address: 'new', symbol: 'gf23525', fav: false},
{id: 6962996031953837, url: '.....', address: 'old', symbol: '435234', fav: false},
{id: 8456218226475281, url: '.....', address: 'old', symbol: '2345', fav: false},
{id: 6759748921116029, url: '.....', address: 'new', symbol: '$B2345ROKE', fav: false}
];
const addr="new";
const list = bookmarks.filter(({address}) => address === addr);
let cnt = 0;
const show = () => {
if (cnt >= list.length) return; // stop the listing
console.log(list[cnt].symbol);
cnt ;
setTimeout(show,2000); // next one
};
show()