How to extract some of the required fields from a json array object to form a new array object

This is a joke. After struggling for a long time, I found that js and ts provide default methods.

Data obtained from the background:

//obtained by server

 data = [ {dd:'22' ,AA:'Dongdong' ,re1:123}, {dd:'33' ,AA:'Lili' ,re1:234}, {dd:'44' ,AA: 'Mingming' ,re1:456} ];

//ideal

[ {dd:'22' ,re1:123}, {dd:'33' ,re1:234}, {dd:'44' ,re1:456} ];

TS solution:

interface myData{

dd:string;

rel:number;

}

const newArr=ref<myData>([])

Get the data, assuming that the get method has been encapsulated:

function getDataList() {

return (baseService.get("/sys/tData/list").then((res) => {

newArr.value = res.data;

console.log('data:',newArr.value)

})

)

}

TS automatically matches the data based on the data defined by the interface, without the need for complex conversions again.

Guess you like

Origin blog.csdn.net/weixin_44821114/article/details/133439653