如何在VueJs 3中发送不同数组的键。

huangapple go评论52阅读模式
英文:

How can I send keys for different arrays in VueJs 3

问题

I have two different Json Array

search_types: {
        0: { name: 'BEGIN ::..', svalue: 1 },
        1: { name: '..:: END', svalue: 2 }
      }

other_dummy_data: {
        0: { id_company: 25, name: 'SHIMIT Co.' },
        1: { id_company: 26, name: 'BIG SHIMIT Co.' }
      }

and I have a Component

<template>
    <select
      :id="input_id"
      :value="modelValue"
      @input="$event => $emit('update:modelValue', $event.target.value)"
      >
    <option v-for="get in optArr" 
      :value="get.opt_value"><!-- THE PROBLEM IS HERE -->
      {{ get.opt_name }}<!-- THE PROBLEM IS HERE -->
    </option>
    </select>
</template>

<script>
export default {
  name: 'InputSelect',
  props: {
    optArr: Array,
    opt_value: String,
    opt_name: String,
    input_id: String,
    modelValue: String
  }
}
</script>

I want to use the same component for two different JSON Arrays. But in the above example, the problem that says is different for each JSON Array.

I tried as below but I am getting an error.

<InputSelect 
v-model="sample_second_selected"
:optarr="search_types" 
:opt_name="name" 
:opt_value="svalue"/>

<InputSelect 
v-model="sample_selected"
:optarr="other_dummy_data" 
:opt_name="name" 
:opt_value="id_company"/>

Could you please help with this?
Thank you in advance for all your help.

英文:

I have two different Json Array

search_types: {
        0: { name: &#39;BEGIN ::..&#39;, svalue: 1 },
        1: { name: &#39;..:: END&#39;, svalue: 2 }
      }

other_dummy_data: {
        0: { id_company: 25, name: &#39;SHIMIT Co.&#39; },
        0: { id_company: 26, name: &#39;BIG SHIMIT Co.&#39; },
      }

and I have a Component

&lt;template&gt;
    &lt;select
      :id=&quot;input_id&quot;
      :value=&quot;modelValue&quot;
      @input=&quot;$event =&gt; $emit(&#39;update:modelValue&#39;, $event.target.value)&quot;
      &gt;
    &lt;option v-for=&quot;get in optArr&quot; 
      :value=&quot;get.opt_value&lt;&lt;-- THE PROBLEM IS HEREM &gt;&gt;&quot; 
      :key=&quot;get&quot;&gt;{{ get.opt_name &lt;&lt;-- THE PROBLEM IS HEREM  }}&lt;/option&gt;
    &lt;/select&gt;

&lt;/template&gt;

&lt;script&gt;
export default {
  name: &#39;InputSelect&#39;,
  props: {
    optArr: Array,
    opt_value: String,
    opt_name: String,
    input_id: String,
    modelValue: String
  }
}
&lt;/script&gt;

I want to use the same component for two different JSON Arrays. But in the above example the probe that says << THE PROBLEM IS HERE >> is different for each JSON Array.

I tried as below but I am getting error.

&lt;InputSelect 
v-model=&quot;sample_second_selected&quot;
:optarr=&quot;search_types&quot; 
:opt_name=&quot;name&quot; 
:opt_value=&quot;svalue&quot;/&gt;


&lt;InputSelect 
v-model=&quot;sample_selected&quot;
:optarr=&quot;other_dummy_data&quot; 
:opt_name=&quot;name&quot; 
:opt_value=&quot;id_company&quot;/&gt;

Could you please help with this?
Thank you in advance for all your help.

答案1

得分: 1

First off, note this is an object with numeric keys, not an array:

{
  0: { name: 'BEGIN ::..', svalue: 1 },
  1: { name: '..:: END', svalue: 2 }
}

this is an array:

[{ name: 'BEGIN ::..', svalue: 1 }, { name: '..:: END', svalue: 2 }]

and a JSON is a string (containing JavaScript Object Notation):

const json = '[{ "name": "BEGIN ::..", "svalue": 1 }, { "name": "..:: END", "svalue": 2 }]'

So what you called a JSON array is neither JSON nor an array. No worries, but these terms are so basic, you will do yourself a huge favor by getting them right.

I think you want to pass in the object or array (Vue's v-for can handle both) and a property name from the objects you loop over (in opt_name).

To resolve a property by name on an object, use bracket notation, i.e. in your template, do:

{{ get[opt_name] }}

If opt_name is a variable containing the string "name", myObject[opt_name] is the same as writing myObject.name.

Since I am doing Mr. smarty pants anyway, some more tips:

  • get is an inappropriate name for an object, not just because "get" is a verb, but because of its specific meaning in programming (I'm actually surprised that it is not a reserved keyword)
  • :key has to be a primitive (basically string or number), but you pass in the object
  • it is customary in Javascript to use camelCase for variables and properties, not snake_case - for most developers, this is not optional

Hope it helps.

英文:

First off, note this is an object with numeric keys, not an array:

{
  0: { name: &#39;BEGIN ::..&#39;, svalue: 1 },
  1: { name: &#39;..:: END&#39;, svalue: 2 }
}

this is an array:

[{ name: &#39;BEGIN ::..&#39;, svalue: 1 }, { name: &#39;..:: END&#39;, svalue: 2 }]

and a JSON is a string (containing JavaScript Object Notation):

const json = &#39;[{ &quot;name&quot;: &quot;BEGIN ::..&quot;, &quot;svalue&quot;: 1 }, { &quot;name&quot;: &quot;..:: END&quot;, &quot;svalue&quot;: 2 }]&#39;

So what you called a JSON array is neither JSON nor an array. No worries, but these terms are so basic, you will do yourself a huge favor by getting them right.


I think you want to pass in the object or array (Vue's v-for can handle both) and a property name from the objects you loop over (in opt_name).

To resolve a property by name on an object, use bracket notation, i.e. in your template, do:

{{ get[opt_name] }}

If opt_name is a variable containing the string &quot;name&quot;, myObject[opt_name] is the same as writing myObject.name.


Since I am doing Mr. smarty pants anyway, some more tips:

  • get is an inappropriate name for an object, not just because "get" is a verb, but because of its specific meaning in programming (I'm actually surprised that it is not a reserved keyword)
  • :key has to be a primitive (basically string or number), but you pass in the object
  • it is customary in Javascript to use camelCase for variables and properties, not snake_case - for most developers, this is not optional

Hope it helps

huangapple
  • 本文由 发表于 2023年4月20日 05:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定