vue.js 如何在索引的数组属性内搜索

6ojccjat  于 2023-01-17  发布在  Vue.js
关注(0)|答案(1)|浏览(120)

我必须检索数组属性中包含特定字符串的应用程序列表:

const users = algoliaClient.initIndex('apps')
return users.search('myId123', { hitsPerPage: 50 });

apps对象如下所示:

{categories: ['an', 'interesting', 'category']}

如何在索引中搜索categories属性包含字符串数组中任何项的项?

pgky5nke

pgky5nke1#

const search = ["cat1", "cat2", "cat3"];
const apps = [
{
name: "app1",
categories: ["cat1", "cat5"],
},
{
name: "app2",
categories: ["cat2", "cat4"],
},
  ];

你是这个意思吗?如果是:

var myIndexes = [];

apps.forEach((app, index) => {
search.every((search) => {
  if (app.categories.includes(search)) {
    myIndexes.push(index);
    return false; // return false here is for stoping the EVERY loop
  } else {
    return true; // keep running the EVERY loop
  }
});
});

相关问题