Hi there, I'm a beginner in JavaScript and I've been experimenting with it. Today, I encountered a problem that I need help with.
The code may seem messy but my goal here is to remove items from idList that are not found in onlinelike. However, I noticed that something was incorrect as the for loop meant to increment i like 0, 1, 2, etc. was actually counting down instead.
Please assist me with this issue.
const idList = ['group1 1', 'group2 2', 'group3 3', 'group1 4'];
const onlinelike = ['group1 1', 'group3 2', 'group2 3', 'group1 4'];
var RemovedList = [];
if (idList != null) {
idList.forEach(item => {
if (onlinelike != null) {
for (i = 0; i < onlinelike.length; i++) {
if (onlinelike[i].split(' ')[0] === item.split(' ')[0]) {
if (onlinelike[i].split(' ')[1] !== item.split(' ')[1]) {
console.log(item, 'to be removed')
console.log(i);
}
}
}
});
}
By the way, when I removed an if statement from it, it started behaving even more oddly by counting like 0, 3, 2, 1, 0, 3.
const idList = ['group1 1', 'group2 2', 'group3 3', 'group1 4'];
const onlinelike = ['group1 1', 'group3 2', 'group2 3', 'group1 4'];
var RemovedList = [];
if (idList != null) {
idList.forEach(item => {
if (onlinelike != null) {
for (i = 0; i < onlinelike.length; i++) {
if (onlinelike[i].split(' ')[0] === item.split(' ')[0]) {
console.log(item, 'to be removed')
console.log(i);
}
}
}
});
}