简体   繁体   English

如何将来自对象的特定键的值存储到数组中 - Vue

[英]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.谢谢您的帮助。

Template模板

<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>

JavaScript JavaScript

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.

相关问题 如何从json对象获取特定的键和值以存储在数据库中? - How to get particular key and value from json object to store in database? 如何根据另一个数组中的值并考虑数组顺序在对象中特定键的值 - How to particular key's value in an object based on the values from another array and using arrays sequence in mind 无法从全局访问由IndexedDB对象存储键的值填充的数组? - Array filled with values from IndexedDB object store key not accessible globally? 如何从对象数组中选择键的所有特定值 - How to select all of a particular values of key from an array of objects 如何将特定键值从 object 转换为数组 javascript - How to convert particular key value from object to array javascript 如何从对象数组中获取特定数据并存储在js(vue js)中的新数组中 - How to fetch particular data from array of objects and store in new array in js (vue js) 从对象数组中计算特定键值 - Count particular key value from array of object Vue 2 - 如何 select 来自数组或 object 的多个值 - Vue 2 - How to select multiple values from an array or object 如何使用AngularJS作为对象中的数组存储清单中的值? - How to store values from a checklist using AngularJS as an array in an object? 使用v-for从对象数组获取JSON键值时出现Vue JS错误 - Vue JS Error when using v-for to get JSON key values from an object array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM