简体   繁体   English

如何通过将数组值与 Vuejs 中的另一个数组进行比较来迭代数组值?

[英]How to iterate array value by comparing it with another array in Vuejs?

HelloWorld.vue HelloWorld.vue

<template>
      <div>
        <div v-for="box in boxes" :key="box.sname">
          <BaseAccordian>
            <template v-slot:title>{{ box.sname }}</template>
            <template v-slot:content>
              <div v-for="paint in paints" :key="paint.tname" class="line">
                <List :content="matchingdata" :sname="box.sname" />
              </div>
            </template>
          </BaseAccordian>
        </div>
      </div>
    </template> 

    <script>
    import BaseAccordian from "./BaseAccordian.vue";
    import List from "./List.vue";
    export default {
      name: "HelloWorld",
      components: {
        BaseAccordian,
        List,
      },
      data() {
        return {
          boxes: [
            {
              sname: "apple",
            },
            {
              sname: "bananna",
            },
            {
              sname: "grapes",
            },
            {
              sname: "choc",
            },
          ],

          paints: [
            {
              tname: "a",
            },
            {
              tname: "b",
            },
            {
              tname: "c",
            },
            {
              tname: "d",
            },
            {
              tname: "e",
            },
          ],

          matchingdata: [
            {
              matchid: "1",
              OverallStatus: "ok",
              sname: "choc",
            },
            {
              matchid: "2",
              OverallStatus: "notok",
              sname: "grapes",
            },
          ],
        };
      },
    };
    </script>

BaseAccordion.vue BaseAccordion.vue

    <template>
      <div class="wrapper">
        <div class="accordion">
          <input type="checkbox" @click="toggleItem" />
          <h2 class="title">
            <slot name="title"></slot>
          </h2>
        </div>
        <div v-show="show" class="content">
          <slot name="content"></slot>
        </div>
      </div>
    </template>
    <script>
    export default {
      components: {},
      data: function () {
        return {
          show: false,
        };
      },
      methods: {
        toggleItem: function () {
          this.show = !this.show;
        },
      },
    };
    </script>

List.vue列表.vue

    <template>
      <div class="">
        <div
          v-for="match in matchingData"
          :key="match.matchid"
          :class="{
            green: match.OverallStatus === 'ok',
            red: match.OverallStatus === 'notok',
          }"
        >
          {{ match.OverallStatus }}
        </div>
      </div>
    </template>
    <script>
    export default {
      components: {},
      props: {
        content: {
          type: Array,
          required: true,
        },
        sname: {
          type: String,
          required: true,
        },
      },
      data: function () {
        return {};
      },
      methods: {},
      computed: {
            matchingData() {
      return this.content.filter((a) => {
        if (a.sname === this.sname) {
          return true;
        } else {
          return false;
        }
      });
    },
      },
    };
    </script>
    <style scoped>
    </style>

I three arrays called matchingdata,boxes,paints array based on this three arrays, i am trying to filter the array.(nested v-for)我根据这三个 arrays 调用了三个 arrays 称为matchingdata、boxes、paints数组,我正在尝试过滤数组。(嵌套 v-for)

Now, I want to iterate the matchingdata array by comparing it with sname in boxes array.现在,我想通过将matchingdata数组与 boxes 数组中的 sname 进行比较来迭代它。 and Common value between matchingdata and boxes array is ""sname"" matchingdata 和 boxes 数组之间的公共值为 ""sname""

I tried above logic, and struck with computed property.我尝试了上面的逻辑,并且对计算属性感到震惊。

Expected Output:-预计 Output:-

In List.vue component, i have {{ match.OverallStatus }} where that field, i want to show,(from the matchingdata array) when user clicked on checkbox.在 List.vue 组件中,当用户单击复选框时,我有 {{ match.OverallStatus }} 那个字段,我想显示(来自 matchingdata 数组)。

Taking the ""sname"" the common value from the matchingdata array and the boxes array从 matchingdata 数组和 boxes 数组中取“sname”的公共值

code:- https://codesandbox.io/s/damp-pine-27s2kn?file=/src/components/List.vue代码:- https://codesandbox.io/s/damp-pine-27s2kn?file=/src/components/List.vue

As you're passing the sname property as a string via a prop to your List.vue component, you'll just need to use that string in your filter function.当您通过 prop 将sname属性作为字符串传递给List.vue组件时,您只需要在过滤器 function 中使用该字符串。

matchingData() {
   return this.content.filter((a) => a.sname === this.sname)
},

I've tried this in your codesandbox link and it is giving some output - but I'm not clear enough on what you're trying to achieve to know if this is the intended outcome.我已经在你的 codesandbox 链接中尝试过这个,它给出了一些 output - 但我不太清楚你想要实现的目标是否是预期的结果。

Just incase you're not aware the 'filter' function returns a new array.以防万一您不知道“过滤器”function 返回一个新数组。 It's not going to return a 'true/false' which I feel you may be trying to do.它不会返回我认为您可能正在尝试做的“真/假”。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

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

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