简体   繁体   English

通过另一个数组Vue js过滤对象中的数组

[英]Filter an array in object by another array Vue js

I have feed with json which has one array with object and object with objects: I am try to filter an object which is in array by another array like:我用 json 提供了一个带有对象的数组和带有对象的对象:我尝试通过另一个数组过滤数组中的对象,例如:

persons = [
    {
        id: 1,
        name: "Karl"
    },
    {
        id: 2,
        name: "Irma"
    },
    {
        id: 3,
        name: "David"
    }
]

roles = {
    0: {
        id:  1,
        title: "admin",    
        persons: [1,2],
        status: 
    },
    1: {
        id:  2,
        title: "editor",    
        persons: [1]
    },
    2: {
        id:  3,
        title: "moderator",    
        persons: [3]
    }
}

I try to get roles by person like this, but it doesn't work我试图通过这样的人来获得角色,但它不起作用

roles() {
    return this.roles.filter((el) => { 
        return this.persons.map((id) => { return id }).includes(el) 
    })
},

get in Vue进入 Vue

<div v-for="(person) in persons">
    <div>
        {{person.name}}
    </div>
    <div v-for="(role) in roles">
        {{role.title}}
    </div>
</div>

I would like to get a result:我想得到一个结果:

Result: 

    Karl
       - admin
       - editor
    Irma
       - admin
    David
       - moderator

You can't use .filter on objects so you have to get the value using Object.values from this.roles .您不能在objects上使用.filter ,因此您必须使用Object.valuesthis.roles获取值。

You are filtering the roles which is incorrect instead, you have to map on the roles and filter the persons if that particulat person.id is present in role.persons您正在过滤不正确的roles ,如果在role.persons存在特定的person.id则必须映射roles并过滤persons

DEMO 演示

getRoles() {
  return Object.values(this.roles).map(({ id, title, persons }) => {
    return {
      id,
      title,
      persons: [...this.persons].filter((p) => persons.includes(p.id)),
    };
  });
},

As always, there will be multiple ways to solve.与往常一样,将有多种方法可以解决。

Consider using array.reduce to iterate over the value of each role before checking if that role 's persons array contains the personId you are searching for.在检查该rolepersons数组是否包含您要搜索的personId之前,请考虑使用array.reduce迭代每个role的值。

 const persons = [{id:1,name:"Karl"},{id:2,name:"Irma"},{id:3,name:"David"}] const roles = {0:{id:1,title:"admin",persons:[1,2],},1:{id:2,title:"editor",persons:[1]},2:{id:3,title:"moderator",persons:[3]}} const getRolesByPersonId = ( personId ) => { return Object.values(roles).reduce((accumulator, currentValue) => { if (currentValue.persons?.includes(personId)) { return [...accumulator, currentValue] } return accumulator }, []) } console.log(getRolesByPersonId(1)) // admin, editor

I'm not so familiar with Vue, but it might be implemented something like this我对 Vue 不太熟悉,但它可能会像这样实现

<template>
  <div class="hello">
    <h1>Result</h1>
    <ul>
      <li v-for="person in persons" :key="person.id">
        <h2>{{ person.name }}</h2>
        <ul>
          <li v-for="role in getRolesByPersonId(person.id)" :key="role.id">
            {{ role.title }}
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      persons: [
        { id: 1, name: "Karl" },
        { id: 2, name: "Irma" },
        { id: 3, name: "David" },
      ],
      roles: {
        0: { id: 1, title: "admin", persons: [1, 2] },
        1: { id: 2, title: "editor", persons: [1] },
        2: { id: 3, title: "moderator", persons: [3] },
      },
    };
  },
  methods: {
    getRolesByPersonId(personId) {
      return Object.values(this.roles).reduce((accumulator, currentValue) => {
        if (currentValue.persons?.includes(personId)) {
          return [...accumulator, currentValue];
        }
        return accumulator;
      }, []);
    },
  },
};
</script>

编辑 sad-snow-1w8yw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM