如何使用字符串数组过滤数组

时间:2020-10-02 07:39:44

标签: javascript

我遇到了一个问题,我在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"];

我想过滤与interests数组具有相同兴趣的用户。

我尝试了不同的方法,但未获得理想的结果。我应该如何进行呢?

5 个答案:

答案 0 :(得分:1)

根据具有至少一个匹配兴趣或所有想要兴趣的用户的期望结果,您可以选择Array#someArray#every来过滤兴趣。

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

答案 1 :(得分: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))

答案 2 :(得分: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);

答案 3 :(得分:0)

使用以下代码

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

答案 4 :(得分:0)

此代码段将返回具有相同兴趣的人员结构。关键是interestName,值是一个人数组。

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