1

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?

3
  • 2
    Can you show an example of what you have tried? It really helps understanding the specifc problem you have and people have a way of starting/thinking. You could add a SO snipper or a jsfiddle.net snipper or something Commented Oct 2, 2020 at 7:40
  • can you show expect result? Commented Oct 2, 2020 at 7:42
  • do you want one or all interests? Commented Oct 2, 2020 at 7:42

5 Answers 5

1

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

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

Comments

0

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

Comments

0

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

Comments

0

use below code

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.