curiouscat
curiouscat

Reputation: 23

How can I filter an array with an array of strings

I am running into an issue, I have a similar array of Strings in JS:

    const users = [
          {
            age: 13,
            username: "adam",
            interests: []
          },
          {
            age: 20,
            username: "eve"
            interests: [
              {
                name: "Bars",
              },
              {
                name: "Cafe",
              },
            ],
          },
          {
            age: 23,
            username: "alex"
            interests: [
              {
                name: "Bars",
              },
              {
                name: "Healthy",
              },
              {
                name: "Japanese",
              },
            ],
          },
        ];
    
    const interests = ["Bars", "Cafe"];

And I would like to filter users having the same interests as the array of interests.

I tried in different ways without getting the desired result. How should I proceed with this?

Upvotes: 1

Views: 97

Answers (5)

Supportic
Supportic

Reputation: 159

This snippet will return a structure of people with the same interests. The key is the interestName and the value is a array of people.

const users = [
  {
    age: 13,
    username: "adam",
    interests: [],
  },
  {
    age: 20,
    username: "eve",
    interests: [
      {
        name: "Bars",
      },
      {
        name: "Cafe",
      },
    ],
  },
  {
    age: 23,
    username: "alex",
    interests: [
      {
        name: "Bars",
      },
      {
        name: "Healthy",
      },
      {
        name: "Japanese",
      },
    ],
  },
];

let commonInterests = new Map();
users.forEach((user) => {
  for (let interest in user.interests) {
    const username = user.username;
    const interestName = user.interests[interest].name;

    if (commonInterests.has(interestName)) {
      let knownNames = commonInterests.get(interestName);
      knownNames.push(username);
      commonInterests.set(interestName, knownNames);
    } else {
      commonInterests.set(interestName, [username]);
    }
  }
});

console.log(Object.fromEntries([...commonInterests]));

Upvotes: 0

Behnam akbari
Behnam akbari

Reputation: 51

use below code

users.filter(e=>e.interests.find(q=>interests.some(w=>w==q.name)))

Upvotes: 0

Alok Takshak
Alok Takshak

Reputation: 280

const users = [
          {
            age: 13,
            username: "adam",
            interests: []
          },
          {
            age: 20,
            username: "eve",
            interests: [
              {
                name: "Bars",
              },
              {
                name: "Cafe",
              },
            ],
          },
          {
            age: 23,
            username: "alex",
            interests: [
              {
                name: "Bars",
              },
              {
                name: "Healthy",
              },
              {
                name: "Japanese",
              },
            ],
          },
        ];
    
    const interests = ["Bars", "Cafe"];
            
 const filteredData = users.filter(user => {
   const userInterests = user.interests.map(interest => interest.name);
   return JSON.stringify(userInterests) === JSON.stringify(interests)
 } );
            
 console.log('data ->', filteredData);

Upvotes: 0

alotropico
alotropico

Reputation: 1994

const users = [
  {
    age: 13,
    username: "adam",
    interests: []
  },
  {
    age: 20,
    username: "eve",
    interests: [
      {
        name: "Bars",
      },
      {
        name: "Cafe",
      },
    ],
  },
  {
    age: 23,
    username: "alex",
    interests: [
      {
        name: "Bars",
      },
      {
        name: "Healthy",
      },
      {
        name: "Japanese",
      },
    ],
  },
];

const interests = ["Bars", "Cafe"];
    
function getUsers(users, interests) {
  return users.map(user => {
    for(let i=0; i<interests.length; i++) {
      return user.interests.some(interest => interest.name == interests[i]) ? user : false
    }
  })
}

console.log(getUsers(users, interests))

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386550

Depending on the wanted result with users with at least one matching interest or all wanted interests, you could take either Array#some or Array#every for filtering interests.

const
    users = [{ age: 13, username: "adam", interests: [] }, { age: 20, username: "eve",  interests: [{ name: "Bars" }, { name: "Cafe" }] }, { age: 23, username: "alex", interests: [{ name: "Bars" }, { name: "Healthy" }, { name: "Japanese" }] }],
    interests = ["Bars", "Cafe"],
    one = users.filter(o => interests.some(i => o.interests.some(({ name }) => name === i))),
    all = users.filter(o => interests.every(i => o.interests.some(({ name }) => name === i)));

console.log(one);
console.log(all);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions