JS通过对象内的嵌套数组过滤对象数组

时间:2021-04-13 02:14:57

标签: javascript arrays reactjs sorting filtering

我有一组与此类似的对象

 let data = [
      {
        name: 'jake fisher',
        age: 24,
        team_ids: [1234, 5678, 9125],
      },

      {
        name: 'kelly smith',
        age: 46,
        team_ids: [3331, 8884, 1234, 8808, 7621],
      },

      {
        name: 'austin williams',
        age: 66,
        team_ids: [9125, 4445],
      },

      {
        name: 'alex rodriguez',
        age: 27,
        team_ids: [],
      },
    ];

我正在尝试将此对象数组过滤为仅包含 team_ids 数为 1234 的对象 所以在这个例子中,仅有两个匹配的团队 ID 是“Jake Fisher”和“Kelly Smith”。

我尝试了一些其他解决方案,有些让我相当接近,但似乎有一些严重的缺陷。我认为真正让我失望的是它是一个嵌套数组(我正在学习,我不经常遇到这个!)任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:-1)

let data = [{
    name: 'jake fisher',
    age: 24,
    team_ids: [1234, 5678, 9125],
  },

  {
    name: 'kelly smith',
    age: 46,
    team_ids: [3331, 8884, 1234, 8808, 7621],
  },

  {
    name: 'austin williams',
    age: 66,
    team_ids: [9125, 4445],
  },

  {
    name: 'alex rodriguez',
    age: 27,
    team_ids: [],
  },
];

const result = data.filter(item => item.team_ids.indexOf(1234) !== -1)
console.log(result);

答案 1 :(得分:-1)

<template>
  <div v-for="component in COMPONENTS_ARRAY" :key="component">
    <!-- I want it to iterate through COMPONENTS_ARRAY and mount them here -->
    <!-- Not working. No errors but doesn't render -->
    {{ component }}
  </div>
</template>

<script>
import AboutPage from './about-page.vue'
import SomePopup from './some-popup.vue'

const COMPONENTS_ARRAY = [
  '<AboutPage :title="title" />',
  '<AboutPage title="This is a Title" />',
  '<SomePopup description="This is a description" />',
  '<SomePopup header-color="red" />'
]

export default {
  props: {
    title: {
      type: String
    }
  },
  data () {
    return {
      COMPONENTS_ARRAY
    }
  },
  components: {
    AboutPage,
    SomePopup
  }
}
</script>