[英]How to store values from a particular key from an object into an array - Vue
I have to say, I am new to the whole Vue framework.我不得不说,我是整个 Vue 框架的新手。 I have created a selectable table.
我创建了一个可选择的表。 The data selected from this table is stored in an object.
从该表中选择的数据存储在一个对象中。 This function should run in the back.
这个函数应该在后面运行。 So, I think I should run it the computed section.
所以,我认为我应该在计算部分运行它。 My object looks like this.
我的对象看起来像这样。 I am trying to retrieve only the ids.
我试图只检索 ID。 This data is stored in the variable selected.
该数据存储在选定的变量中。
[ { "id": 5, "name": "CD", "box": "A5", "spot": 1 }, { "id": 2, ""name": "DVD", "box": "A2", "spot": 1 } ]
I would like to only retrieve the values from the key id.我只想从密钥 ID 中检索值。 These values should be stored in an array.
这些值应该存储在一个数组中。 The array should be pushed when the submit button is clicked.
单击提交按钮时应推送该数组。 I will later use this array to store this information in this array into a database.
稍后我将使用此数组将此数组中的信息存储到数据库中。
You can find my code below.你可以在下面找到我的代码。 I guess I am missing something because it doesn't seem to work.
我想我错过了一些东西,因为它似乎不起作用。 How should I refer to this function that it runs automatically and that the array can be called in my controller to put it into the database?
我应该如何引用这个自动运行的函数,并且可以在我的控制器中调用该数组以将其放入数据库? Thanks for the help.
谢谢您的帮助。
<div class="row">
<div class="col-2">
<b-form-input v-model="date" class="form-control" type="date" value="getDate" ></b-form-input>
<pre class="mt-3 mb-0">{{ date }}</pre>
</div>
<div class="col-6">
<b-form-input v-model="description" placeholder="Enter some text"></b-form-input>
<pre class="mt-3 mb-0">{{ description }}</pre>
</div>
<!-- Submit data to db -->
<div class="col-4">
<button class="btn btn-primary custom-button-width" center-block>Request antibody pool</button>
</div>
</div>
data() {
return {
// data from db
data: this.testerdata,
// selected is the object where my selected data is stored.
selected: [],
// here should my id end up
selectedids: {},
description: '',
date: '',
}
},
computed: {
tester() {
var array = [];
for (var test in this.selected) {
if (test == "id") {
array += this.selected[test];
}
}
console.log(array);
}
},
methods: {
storeData: async function() {
axios.post('/panel', {
description: this.description,
date: this.date,
selectedids: this.tester(selectedids)
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
}).finally(function() {
// always executed
});
},
}
The problem is with this line:问题出在这一行:
array += this.selected[test];
This the way to add an item to an array:这是将项目添加到数组的方法:
array.push(this.selected[test]);
Additionally, you are trying to loop over properties of this.selected
, but it is an array which contains objects and has no properties.此外,您正在尝试遍历
this.selected
属性,但它是一个包含对象且没有属性的数组。 Instead, you want to loop over each object item in the array and push
the id
property.相反,您希望遍历数组中的每个对象项并
push
id
属性。
And, since this.selected
is an array, not an object, you should not use a for...in
loop-- which is for objects-- because the order will not be guaranteed.而且,由于
this.selected
是一个数组,而不是一个对象,因此您不应该使用for...in
循环——这是用于对象的——因为顺序将无法保证。 You can read more about that in the MDN for...in
docs .您可以在MDN
for...in
docs 中阅读更多相关信息。
You could use a normal for
loop or the array's forEach
loop.您可以使用普通的
for
循环或数组的forEach
循环。 I'll show the forEach
:我将展示
forEach
:
this.selected.forEach(obj => {
array.push(obj.id);
});
I understand that you might be looking for vanilla Javascript answer here.我知道您可能在这里寻找普通的 Javascript 答案。 However a lot of code can be saved if you can use RamdaJS library to solve such issues.
但是,如果您可以使用RamdaJS库来解决此类问题,则可以节省大量代码。
It has an excellent set of functions like pluck , which will do the same thing in 1 line.它有一组很棒的函数,比如pluck ,它们可以在 1 行中做同样的事情。
let array = R.pluck('id')(this.selected);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.