【问题标题】:Needing VueJS Getter to only return all values of the last JSON property as an array需要 VueJS Getter 仅将最后一个 JSON 属性的所有值作为数组返回
【发布时间】:2019-07-21 12:01:07
【问题描述】:

我正在使用 Django REST 后端将 API 数据发送到 VueJS 前端。 我正在尝试将每日计数放在 ChartJS 图表中。

import { HorizontalBar } from '../BaseCharts'

export default {
  extends: HorizontalBar,
  data() {
    return{
    }
  },
  mounted () {
    /* Need to write the API call for chart data here. */
    this.$store.dispatch("DailyCountAction")

this.renderChart({
  labels: [this.$store.state.DailyCount],
  datasets: [
    {
      label: 'Existing Patients',
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
    ],
    borderColor: [
      'rgba(255,99,132,1)',
      'rgba(54, 162, 235, 1)',
      'rgba(255, 206, 86, 1)',
      'rgba(75, 192, 192, 1)',
      'rgba(153, 102, 255, 1)',
      'rgba(255, 159, 64, 1)'
  ],
  borderWidth: 1,
      data: [4,8,2,]
    }, 

您可以在“标签”字段中看到我有一个返回数据的 getter:

 {
        "Check_In_Count": "7",
        "Average_Time_Spent": "3",
        "Average_Wait_Time": "2",
        "Cancelations": "0",
        "New_Patient_Count": "4",
        "Existing_Patient_Count": "3",
        "Current_Cycle_Date": "2062019"
    },
    {
        "Check_In_Count": "4",
        "Average_Time_Spent": "8",
        "Average_Wait_Time": "6",
        "Cancelations": "0",
        "New_Patient_Count": "1",
        "Existing_Patient_Count": "3",
        "Current_Cycle_Date": "2072019"
    },
    {
        "Check_In_Count": "7",
        "Average_Time_Spent": "3",
        "Average_Wait_Time": "9",
        "Cancelations": "0",
        "New_Patient_Count": "0",
        "Existing_Patient_Count": "7",
        "Current_Cycle_Date": "2082019"
    },
    {
        "Check_In_Count": "8",
        "Average_Time_Spent": "8",
        "Average_Wait_Time": "1",
        "Cancelations": "0",
        "New_Patient_Count": "4",
        "Existing_Patient_Count": "4",
        "Current_Cycle_Date": "2092019"
    },

我只需要将所有 Current_Cycle_Date 值返回到进入“标签”字段的数组中。我对如何解决这个问题感到非常困惑。

最后,标签字段应如下所示: 标签:['2092019', '2082019', '2072019', '2062019']

我已经看到了在 getter 中使用 MAP 的示例。到目前为止没有任何效果。这可以使用方法中的一些逻辑来完成吗?

任何建议将不胜感激!

【问题讨论】:

  • 如果您只想从后端返回Current_Cycle_Date,请在问题中添加一些代码。如果你想在前端做,我认为@muka.gergely 已经发布了正确的答案

标签: javascript django vue.js vuex


【解决方案1】:

也许您可以尝试将其作为数组返回:

const data = [{
        "Check_In_Count": "7",
        "Average_Time_Spent": "3",
        "Average_Wait_Time": "2",
        "Cancelations": "0",
        "New_Patient_Count": "4",
        "Existing_Patient_Count": "3",
        "Current_Cycle_Date": "2062019"
    },
    {
        "Check_In_Count": "4",
        "Average_Time_Spent": "8",
        "Average_Wait_Time": "6",
        "Cancelations": "0",
        "New_Patient_Count": "1",
        "Existing_Patient_Count": "3",
        "Current_Cycle_Date": "2072019"
    },
    {
        "Check_In_Count": "7",
        "Average_Time_Spent": "3",
        "Average_Wait_Time": "9",
        "Cancelations": "0",
        "New_Patient_Count": "0",
        "Existing_Patient_Count": "7",
        "Current_Cycle_Date": "2082019"
    },
    {
        "Check_In_Count": "8",
        "Average_Time_Spent": "8",
        "Average_Wait_Time": "1",
        "Cancelations": "0",
        "New_Patient_Count": "4",
        "Existing_Patient_Count": "4",
        "Current_Cycle_Date": "2092019"
    }]


var ccd = data.map( i => i.Current_Cycle_Date )

console.log( ccd )

【讨论】:

  • 我的数据来自一个 Action Axios 调用我没有在前端硬编码它,但这似乎是解决它的最佳解决方案。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 2017-09-29
  • 2022-10-05
  • 2015-09-01
  • 1970-01-01
  • 2019-01-27
  • 2014-04-28
相关资源
最近更新 更多