简体   繁体   English

Vue.js 从数组中弹出一个嵌套对象

[英]Vue.js pop a nested object from an array

Working on a quick dashboard and I want to remove the second Object from departments_by_dept_emp .在快速仪表板上工作,我想从departments_by_dept_emp删除第二个对象。 How would this be best achievable with Vue?使用 Vue 如何最好地实现这一点?

HTML HTML

<tbody id="rows" v-for="result in results">
   <tr>
     <td>{{ result.first_name }} {{ result.last_name }}</td>
     <td v-for="department in result.departments_by_dept_emp">{{ department.dept_name }}</td>
     <td>{{ result.hire_date }}</td>
     <td>{{ result.birth_date }}</td>
   </tr>
</tbody>

JavaScript JavaScript

   {
   "emp_no": 10010,
      "birth_date": "1963-06-01",
      "first_name": "Duangkaew",
      "last_name": "Piveteau",
      "gender": "F",
      "hire_date": "1989-08-24",
      "departments_by_dept_emp": [
        {
          "dept_no": "d004",
          "dept_name": "Production"
        },
        {
          "dept_no": "d006",
          "dept_name": "Quality Management"
        }
      ]
    }

Axios call getting the data is stored in an empty array axios 调用获取数据存储在空数组中

data () {
    return {
      results: [],
    }
}

Axios call axios 调用

getData() {
      this.loading = true

      axios.get('https://url', { 'headers': { 'Api-Key': '' } })
          .then(response => {
            this.results = response.data.resource
            this.next = response.data.meta.next
            this.loading = false
            console.log(response.data.resource)
          })
          .catch(error => {
            console.log(error)
          })
      }

So from the example, essentially I only want the Production department name and remove Quality Management from my table.因此,从示例中,基本上我只需要生产部门名称并从我的表中删除质量管理。 I do not believe slice() will do the trick as it will only clone one dimension.我不相信 slice() 会成功,因为它只会克隆一维。 Also, anything like splice() or findIndex I receive the error "Cannot read property '{example}' of undefined""此外,像 splice() 或 findIndex 之类的东西我收到错误“无法读取未定义的属性 '{example}'””

If you only want to display an element from an array you can pass the index of the element to display如果只想显示数组中的元素,则可以传递元素的索引以显示

 var app = new Vue({ el: '#app', data: { results: [{ "emp_no": 10010, "birth_date": "1963-06-01", "first_name": "Duangkaew", "last_name": "Piveteau", "gender": "F", "hire_date": "1989-08-24", "departments_by_dept_emp": [ { "dept_no": "d004", "dept_name": "Production" }, { "dept_no": "d006", "dept_name": "Quality Management" } ] }] } })
 <script src="https://unpkg.com/vue"></script> <div id="app"> <table> <tbody id="rows" > <tr v-for="result in results"> <td>{{ result.first_name }} {{ result.last_name }}</td> <td>{{ result.departments_by_dept_emp[0].dept_name }}</td> <td>{{ result.hire_date }}</td> <td>{{ result.birth_date }}</td> </tr> </tbody> </table> </div>

Hope this works for you.希望这对你有用。

Since vue.js data objects are simply javascript objects, it should be possible to use array.pop().由于 vue.js 数据对象只是 javascript 对象,因此应该可以使用 array.pop()。

You should be able to add a function like this to your methods block:您应该能够将这样的函数添加到您的方法块中:

popDept: function() {
   return this.departments_by_dept_emp.pop();
},

This function will both remove the department from the list, and return it.此函数将从列表中删除部门,并将其返回。

At risk of looking like writing some code for you...冒着看起来像为你写一些代码的风险......

Vue is a javascript framework so you can write functions in plain javascript to find an item in an array and then remove it. Vue 是一个 javascript 框架,因此您可以用普通的 javascript 编写函数来查找数组中的项目,然后将其删除。 So in your template where you iterate through department.dept_name you could add a click listener, @click="removeElement(department.dept_name)"因此,在您遍历department.dept_name模板中,您可以添加一个点击侦听器, @click="removeElement(department.dept_name)"

Then in your script section:然后在您的脚本部分:

export default {
data: () => ({
  results: {
  "emp_no": 10010,
     "birth_date": "1963-06-01",
     "first_name": "Duangkaew",
     "last_name": "Piveteau",
     "gender": "F",
     "hire_date": "1989-08-24",
     "departments_by_dept_emp": [
       {
         "dept_no": "d004",
         "dept_name": "Production"
       },
       {
         "dept_no": "d006",
         "dept_name": "Quality Management"
       }
     ]
   }
}),
methods: {
  removeElement(x) {
    var ind = this.results.departments_by_dept_emp.findIndex(n => n.dept_name === x)  
    this.results.departments_by_dept_emp.splice(ind, 1)
}

That will remove the object from the array and leave the rest as is.这将从数组中删除对象并保留其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM