Home > front end >  how can I sort through an ☺how can i compare two arrays and log same or different values
how can I sort through an ☺how can i compare two arrays and log same or different values

Time:09-26

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.

  1. Split all fruits in fruitsToCheck with the symbol ,. ==> done
  2. Check for all fruits in the resulting array if they are included in 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);

CodePudding user response:

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 :)

CodePudding user response:

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);
      }
    }

CodePudding user response:

"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.

  1. Basic for loop, and indexOf:

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);

  1. 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);

  1. Using 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);

  • Related