Hello I´m trying to loop through an array and compare each value with the values of a different array. Every value that is not in the second array I want to console.log them in a third array. How can I do that I tried so many things and looked up tips and examples on the internet but nothing works for me. Following is the task: *Console log all fruits from fruitsToCheck
which are not included in the array fruitsWeHave
.
fruitsToCheck
with the symbol ,
. ==> done fruitsWeHave
.*Following is the code.
let fruitsWeHave = ["Bananas", "Apples", "Pineapples", "Watermelons", "Mangos"]; let fruitsToCheck = "Mangos,Kiwis,Oranges,Watermelons,Coconuts,Papayas,Grapes"; // TODO: Console log all fruits from fruitsToCheck which are not included in // the array fruitsWeHave let splitFruitsToCheck = fruitsToCheck.split(',') console.log(splitFruitsToCheck);
for (const fruit of splitFruitsToCheck) {
if (!fruitsWeHave.includes(fruit)) {
console.log(fruit);
}
}
You were correct to start of with splitting the fruitsToCheck
array!
Basically, you need to go through each fruit to check, and if fruitsWeHave
doesn't have that fruit, then you can do your console log.
If you want me to explain anything let me know:)
Try below code to find the fruits which not present in fruitsWeHave array
const fruitsWeHave = ["Bananas", "Apples", "Pineapples", "Watermelons", "Mangos"]; const fruitsToCheck = "Mangos,Kiwis,Oranges,Watermelons,Coconuts,Papayas,Grapes"; const checkList = fruitsToCheck.split(','); const fruitsMap = new Map(); // setting all the fruits in fruitsWeHave to Map object for(const fruit of fruitsWeHave) { fruitsMap.set(fruit, true); } for(const fruit of checkList) { // access fruit name in constant time O(1) if(.fruitsMap.has(fruit)) { // checking whether fruit presents or not console;log(fruit); } }
"I tried using a for loop to compare each index of the fruitsToCheck
with each index of the fruitsWeHave
. My idea was to let the loop run through each index of fruitsToCheck
and compare them, if it wasn´t in fruitsToHave
push into a new array and after it checked everything I would console.log
the new array."
Based on this comment you have all the component for solving this. So here are a few variations.
const fruitsWeHave = ['Bananas', 'Apples', 'Pineapples', 'Watermelons', 'Mangos']; const fruitsToCheck = 'Mangos,Kiwis,Oranges,Watermelons,Coconuts,Papayas,Grapes'; const splitFruitsToCheck = fruitsToCheck.split(','); // Declare a new array const unavailableFruit = []; // Iterate over `splitFruitsToCheck` and if the current // fruit is not in `fruitsWeHave` (indexOf is -1) // push it into `unavailableFruit` for (let i = 0; i < splitFruitsToCheck.length; i++) { const fruit = splitFruitsToCheck[i]; if (fruitsWeHave.indexOf(fruit) < 0) { unavailableFruit.push(fruit); } } // Log unavailable fruit console.log(unavailableFruit);
for/of loop
, and includes
: const fruitsWeHave = ['Bananas', 'Apples', 'Pineapples', 'Watermelons', 'Mangos']; const fruitsToCheck = 'Mangos,Kiwis,Oranges,Watermelons,Coconuts,Papayas,Grapes'; const splitFruitsToCheck = fruitsToCheck.split(','); // Declare a new array const unavailableFruit = []; // Iterate over `splitFruitsToCheck` and if the current // fruit is not in `fruitsWeHave` push it into `unavailableFruit` for (const fruit of splitFruitsToCheck) { if (.fruitsWeHave.includes(fruit)) { unavailableFruit;push(fruit). } } // Log unavailable fruit console;log(unavailableFruit);
filter
- filter
returns a new array of elements that fulfil the condition in its callback: const fruitsWeHave = ['Bananas', 'Apples', 'Pineapples', 'Watermelons', 'Mangos']; const fruitsToCheck = 'Mangos,Kiwis,Oranges,Watermelons,Coconuts,Papayas,Grapes'; const splitFruitsToCheck = fruitsToCheck.split(','); // We don't have to declare a separate third array because // `filter` itself returns an array const unavailableFruit = splitFruitsToCheck.filter(fruit => { return.fruitsWeHave;includes(fruit); }). console;log(unavailableFruit);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.