1

I have a two dimensional array:

array = [["car","dog","kenya"],["plane", "cat", "kenya"],["boat", "mouse", "england"]]

I would like to filter by country and return all the animals "associated" with that country in another array.

For example filtering by Kenya places dog and cat into another array.

Hope that makes sense. Any help would be most appreciated. Thanks.

2
  • 4
    do you have tried something? please add your code. Commented Nov 22, 2018 at 9:50
  • 1
    Arrays have keys: We normally call them indexes. In your example, the country appears to always be at index 2. Commented Nov 22, 2018 at 9:50

5 Answers 5

1

Assuming that your animal is always at index 1 you can use can use Array.prototype.filter() and Array.prototype.map() to achieve this.

.filter() will "filter" your 2d array to only include arrays which have the country within it, then .map() will convert this 2d array to a 1d array by "replacing" all the inner arrays to be the animal (ie index 1).

See working example below:

const array = [["car","dog","kenya"],["plane", "cat", "kenya"],["boat", "mouse", "england"]],
      filterBy = "kenya", // The country to filter by
      
      animals = array.filter(arr => arr[2] == filterBy).map(elem => elem[1]);

console.log(animals);

Sign up to request clarification or add additional context in comments.

Comments

0

You could filter the array and then map the wanted item.

This solution take the advantage of destructuring assignment, where an array is taken and the items gets a name.

var array = [["car", "dog", "kenya"], ["plane", "cat", "kenya"], ["boat", "mouse", "england"]],
    result = array
        .filter(([,, country]) => country === "kenya")
        .map(([, animal]) => animal)

console.log(result);

Comments

0

Looking at the code you have provided and considering that the country is always in index-2 and animals are in index-1 you can use a forEach() loop to get animals for that particular country.

var array = [
  ["car", "dog", "kenya"],
  ["plane", "cat", "kenya"],
  ["boat", "mouse", "england"]
];
var country = 'kenya';
var animals = [];
array.forEach((item) => {
  if (item[2] === country) {
    animals.push(item[1]);;
  }
});
console.log(animals);

Comments

0

You can create a generic function which gets the index of what type you want to filter ('2' for countries) and the value you want to match ('kenya' for example)

array = [["car","dog","kenya"],["plane", "cat", "kenya"],["boat", "mouse", "england"]]
const FILTER_TYPE = { vehicle: 0, animal: 1, country: 2 }

const filter_by = (index, value) => (array.filter(item => (item[index] === value)).map(item => item[FILTER_TYPE.animal]))

console.log(filter_by(FILTER_TYPE.country, 'kenya'))

Comments

0

Here in country kenya you can find the related animals, of that country. Let me know, if any changes require.

array = [["car","dog","kenya"],["plane", "cat", "kenya"],["boat", "mouse", "england"]]
    array.map((data,i) => {
        data.map((dataKey)=>{
        if(dataKey === "kenya"){
        console.log(array[i])
        }
    })
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.