這是輸出:輸出的資料結構是Map<String, List<Pair<String, String>>>
"testdata": [
{
"1.0": "True"
},
{
"1.1": "False"
}
]
現在我需要在 UI 上將此資料顯示為"testdata":["1.0","1.1","1.2"],其中從 Pair 中,我只想從 Pair 中獲取第一個元素并將它們放入結構中Map<String, List<String>>
那么如何在 javascript 中撰寫該代碼以獲得該輸出呢?
this.previousData = versions.filter(v => v !== this.version).map(item => ({ text: item, value: item }))
如何修改此代碼以獲得此輸出"testdata":["1.0","1.1","1.2"]?
uj5u.com熱心網友回復:
你可以嘗試這樣的事情:
// This assumes that there will be only one key in each array item, or the first key in each array item denotes the version
this.previousData =
versions
.filter(v => v !== this.version)
.map(item => Object.keys(item)[0])
// or as MikeT suggested, if you are only expecting something like this:
// [
// { "v1": "some value 1" },
// { "v2": "some value 2" },
// ... in general --> { "version": "some value" }
// ]
// you may try this as well
this.previousData =
versions
.filter(v => v !== this.version)
.map(item => Object.keys(item))
.flat()
uj5u.com熱心網友回復:
你的問題不太清楚你正在努力解決哪個元素
所以要獲取資料
async getData()
{
const resp = await fetch("<<your url from your java service>>" )
return await resp.json();
}
按照您希望的方式格式化資料
formatData(json){
return json.testdata.map((i)=>Object.entries(i).map(([k,v])=>k)).flat()
}
并在 vue 中顯示
<template>testdata:{{testdata}}</template>
<script>
...
methods:{
async populatedata(){
const tmp = await getData()
this.testdata = formatData(tmp)
}
}
...
</script>
Object.entries 將一個物件轉換為一個元組陣列 so {"1.0":"True","1.1":"False"}will become [["1.0","True"],["1.1":"False"]]which 然后讓您使用元組分解來獲取鍵
如果你不需要這些值,你也可以使用 Object.keys() 但從背景關系中不清楚,所以我給了你更靈活的選擇
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/460836.html
標籤:javascript Vue.js
上一篇:VueJS檔案上傳欄位無法重置
下一篇:洗掉所有特殊的連續字符
