英文:
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: 'BEGIN ::..', svalue: 1 },
1: { name: '..:: END', svalue: 2 }
}
other_dummy_data: {
0: { id_company: 25, name: 'SHIMIT Co.' },
0: { 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 HEREM >>"
:key="get">{{ get.opt_name <<-- THE PROBLEM IS HEREM }}</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 probe that says << THE PROBLEM IS HERE >> is different for each JSON Array.
I tried as below but I am getting 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.
答案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: '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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论