skip to Main Content

I have the following javascript array, in which I need to return the matching city names.

[['75', 'Paris'], ['456', 'Marseille'], ['634', 'Toulouse'], ['668', 'Lyon'], ['035', 'Saint-Étienne']] 

When input is 634, the javascript function will return Toulouse.
When input is 634, 75, then the return will be a list of comma separated cities. ‘Toulouse, Paris’
When input is 634, 75, 668 then the return will be a list of comma separated cities. ‘Toulouse, Paris, Lyon’

Any ideas?

3

Answers


  1. To achieve this, you can write a JavaScript function that takes an array of city pairs (each pair being an array with an ID and a city name) and a list of IDs as input. The function then filters the city pairs based on whether their IDs match any of the input IDs, maps the matching pairs to their city names, and joins these names into a comma-separated string. Below is a function that implements this logic:

    function findCities(cityPairs, ...inputIds) {
      // Filter the cityPairs to find matches, map to get city names, and join with commas
      return cityPairs
        .filter(pair => inputIds.includes(pair[0])) // Check if the city ID is in the input IDs
        .map(pair => pair[1]) // Extract the city name
        .join(', '); // Join the names into a comma-separated string
    }
    
    // Example city pairs array
    const cityPairs = [
      ['75', 'Paris'], 
      ['456', 'Marseille'], 
      ['634', 'Toulouse'], 
      ['668', 'Lyon'], 
      ['035', 'Saint-Étienne']
    ];
    
    // Example usage
    console.log(findCities(cityPairs, '634')); // Toulouse
    console.log(findCities(cityPairs, '634', '75')); // Toulouse, Paris
    console.log(findCities(cityPairs, '634', '75', '668')); // Toulouse, Paris, Lyon
    

    This function uses the filter method to keep only the city pairs where the ID matches one of the input IDs. It then uses map to transform the filtered array of pairs into an array of city names. Finally, it uses join to concatenate all city names into a single string separated by commas.

    The function findCities is defined to take multiple inputIds using rest parameters (...inputIds), allowing you to pass one or more IDs as separate arguments rather than needing an array. This makes the function flexible and easy to use with different numbers of input IDs.

    Login or Signup to reply.
  2. You can use the Array.find() method to search for a matching city for each input number. If a match is found, it adds the corresponding city name to the matchingCities array. Then you can join to create a comma-separated string of the matching cities.

    const cityData = [['75', 'Paris'], ['456', 'Marseille'], ['634', 'Toulouse'], ['668', 'Lyon'], ['035', 'Saint-Étienne']];
    
    function findCities(...inputNumbers) {
      const matchingCities = [];
      inputNumbers.forEach((number) => {
        const city = cityData.find((entry) => entry[0] === number);
        if (city) {
          matchingCities.push(city[1]);
        }
      });
      return matchingCities.join(', ');
    }
    
    
    console.log(findCities('634')); 
    console.log(findCities('634', '75')); 
    console.log(findCities('634', '75', '668'));
    Login or Signup to reply.
  3. You can use Object.fromEntries to make a lookup object, like this:

    const data = Object.fromEntries([['75', 'Paris'], ['456', 'Marseille'], ['634', 'Toulouse'], ['668', 'Lyon'], ['035', 'Saint-Étienne']]);
    
    function lookup(...ids) {
      return ids.map(id => data[id]).join(', ')
    }
    
    console.log(lookup(634, 75))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search