我正在將物件資料發送到一些 api,我有很多狀態,并不是每次都需要每個人,所以,如果它是 null 或空字串,我想從物件中洗掉一些資料,我該怎么做?
export default {
methods: {
sendData() {
axios
.post("api", this.$store.state.data)
.then((response) => console.log(response))
.catch((error) => console.log(error));
console.log(this.$store.state.data);
},
},
mounted() {
this.sendData();
},
};
在這里,我存盤狀態資料,我只需要發送填充值,而不是所有空值,我怎么能實作這一點?
uj5u.com熱心網友回復:
嘗試使用應用于物件鍵的 reduce 方法回傳具有非空欄位的物件:
export default {
methods: {
sendData() {
let filledData = Object.keys(this.$store.state.data).reduce((acc,curr)=>{
if(data[curr]){
acc[curr]=data[curr]
}
return acc;
},{})
axios
.post("https://covid19.devtest.ge/api/create", filledData )
....
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/493318.html
標籤:javascript api Vue.js
上一篇:如何自定義格式數字
