Avoid using non-primitive value as key, use string

2019-08-25 18:42发布

问题:

I'm trying to build an application where I'm trying to get values, everything was working fine,

Here is my code: https://codeshare.io/aY7rX3

But suddenly some error started coming:

Avoid using non-primitive value as key, use string/number value instead

Somewhere around:

<div class="col-sm-4 border-right">
    <div>
        <button @click.prevent="" v-for="(obj, key) in tags"
                :key="key"
                class="btn btn-rounded btn-sm"
                :class="tagParentClass(key)">
            {{key}}
        </button>
    </div>
</div>

The data set of tags

export const tags = {
    Investor: [
        {display: "Mutual Fund", value: 'Mutual Fund'},
        {display: "Insurance", value: 'Insurance'},
        {display: "FII", value: 'FII'},
        {display: "PMS", value: 'PMS'},
        {display: "Proprietary", value: 'Proprietary'},
        {display: "HNI", value: 'HNI'},
        {display: "Private Equity", value: 'Private Equity'},
        {display: "Others", value: 'Others'}
    ],
    Research: [
        {display: "Global", value: 'Global'},
        {display: "Domestic", value: 'Domestic'},
        {display: "Retail", value: 'Retail'},
        {display: "Others", value: 'Others'}
    ],
    Corporate: [
        {display: "Corporate", value: 'Corporate'}
    ],
    Others: [
        {display: "Debt", value: 'Debt'},
        {display: "Debt Adviser", value: 'Debt Adviser'},
        {display: "Investment Banker", value: 'Investment Banker'},
        {display: "Media", value: 'Media'},
        {display: "Others", value: 'Others'}
    ]
}

Help me out in this.

回答1:

Try changing the JSON format of tags.
The warning message will disappear if you modify your JSON Format to below format

[{
    Investor: [
        {display: "Mutual Fund", value: 'Mutual Fund'},
        {display: "Insurance", value: 'Insurance'},
        {display: "FII", value: 'FII'},
        {display: "PMS", value: 'PMS'},
        {display: "Proprietary", value: 'Proprietary'},
        {display: "HNI", value: 'HNI'},
        {display: "Private Equity", value: 'Private Equity'},
        {display: "Others", value: 'Others'}
    ]
    },
   { 
     Research: [
        {display: "Global", value: 'Global'},
        {display: "Domestic", value: 'Domestic'},
        {display: "Retail", value: 'Retail'},
        {display: "Others", value: 'Others'}
    ]
    },
   { 
     Corporate: [
        {display: "Corporate", value: 'Corporate'}
    ]
    },
    {
      Others: [
        {display: "Debt", value: 'Debt'},
        {display: "Debt Adviser", value: 'Debt Adviser'},
        {display: "Investment Banker", value: 'Investment Banker'},
        {display: "Media", value: 'Media'},
        {display: "Others", value: 'Others'}
    ]
    }
  ]

Updated:

i have updated your JSON format to a more friendlier JSON Format that is used in most usecases.
Try this approach and let me know if it works

Template

<div v-for="(obj,index) in tags" :key="index">
              {{index}} {{obj.topic}}
        <div style="padding-left: 20px;" v-for="(category,index1) in obj.category" :key="index1">
                {{index1}} == {{category.display}} || {{category.value}}
              </div>
          </div>

Script

 export default {
      data () {
        return {
          tags : 
            [
              {
              topic : "Investor",
              category : [
                {display: "Mutual Fund", value: 'Mutual Fund'},
                {display: "Insurance", value: 'Insurance'},
                {display: "FII", value: 'FII'},
                {display: "PMS", value: 'PMS'},
                {display: "Proprietary", value: 'Proprietary'},
                {display: "HNI", value: 'HNI'},
                {display: "Private Equity", value: 'Private Equity'},
                {display: "Others", value: 'Others'}
              ]
              },
              { 
                topic : "Research",
                category : [
                  {display: "Global", value: 'Global'},
                  {display: "Domestic", value: 'Domestic'},
                  {display: "Retail", value: 'Retail'},
                  {display: "Others", value: 'Others'}
                ]
              },
              { 
                topic : "Corporate" ,
                category : [
                 {display: "Corporate", value: 'Corporate'}
                ]
              },
              {
                topic : "Others", 
                category : [
                  {display: "Debt", value: 'Debt'},
                  {display: "Debt Adviser", value: 'Debt Adviser'},
                  {display: "Investment Banker", value: 'Investment Banker'},
                  {display: "Media", value: 'Media'},
                  {display: "Others", value: 'Others'}
                ]
              }
            ]
         }
      }
    }


回答2:

in object tags you have nested objects in the array

if you want pass all tags

let tags  = {
    Investor: [
        {display: "Mutual Fund", value: 'Mutual Fund'},
        {display: "Insurance", value: 'Insurance'},
        {display: "FII", value: 'FII'},
        {display: "PMS", value: 'PMS'},
        {display: "Proprietary", value: 'Proprietary'},
        {display: "HNI", value: 'HNI'},
        {display: "Private Equity", value: 'Private Equity'},
        {display: "Others", value: 'Others'}
    ],
    Research: [
        {display: "Global", value: 'Global'},
        {display: "Domestic", value: 'Domestic'},
        {display: "Retail", value: 'Retail'},
        {display: "Others", value: 'Others'}
    ],
    Corporate: [
        {display: "Corporate", value: 'Corporate'}
    ],
    Others: [
        {display: "Debt", value: 'Debt'},
        {display: "Debt Adviser", value: 'Debt Adviser'},
        {display: "Investment Banker", value: 'Investment Banker'},
        {display: "Media", value: 'Media'},
        {display: "Others", value: 'Others'}
    ]
}

new Vue({
  el: '#app',
  data: {
    tags 
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="app">
  <div class="col-sm-4 border-right">
    <div v-for="(tag, key) in tags">{{key}}
        <button @click.prevent="" 
                v-for="key in tag"
                :key="key">
            {{key.value}}
        </button>
    </div>
  </div>
</div>