vue 动态修改后端请求_如何从vue对象动态提取数据以向后端发送POST请求?

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 15:11   11   0

I have a vue object with all these getters and setters, here is a screenshot from the console.log:

The structure of the actual DATA (the not-vue stuff) looks like this:

{

Internal_key: "TESTKEY_1",

extensiontable_itc: {

description_itc: "EXTENSION_ITC_1_1",

description_itc2: "EXTENSION_ITC_1_2",

},

extensiontable_sysops: {

description_sysops: "EXTENSION_SYSOPS_1"

}

}

The data might look different in other usecases.

There might be more or less key-value pairs on the outer object, and the keys might be named differently as well. Same goes for the nested objects and their contents.

Is there some convenient way to extract this data into a plain JS Object?

If not, how can I best loop the vue object to extract the data "manually"?

The AJAX request shall be performed by an axios request, if this is important as well.

EDIT:

Here is the relevant data in vue:

data() {

return {

editingRecord: {

original: null,

copy: null

}

}

}

During my programflow, both editingRecord.orginal and editingRecord.copy receive data from an inputform. copy sets its data to original if the user clicks the save/send button. Then, I want to take the data from editingRecord.original with both its keys and values and send them to the server via AJAX request.

解决方案

Okay, I found the solution.

let vueObject = Object.entries(this.editingRecord.original)

for(const[key, value] of vueObject){

if(typeof value == 'object' && value !== null){

for(const [nestedKey, nestedValue] of Object.entries(value)){

result[nestedKey] = nestedValue

}

}else{

result[key] = value

}

}

console.log("resultObject is", result)

This way you can iterate over all properties including the properties of nested objects and reassign both key and value to a fresh, one-dimensional array.

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP