1. In the object array, according to an attribute of each object in the array, add a new field in the object, and assign different values according to the state of the attribute
Scene 1:
- Description: Only unit price and quantity are returned in the background. The logic needs to calculate the total price of each commodity according to the quantity and unit price
- Scene graph:
// The data returned by the background
res.data=[{num:2.price:238}, {num:4.price:213},... ]// We need the data
data = [{num:2.price:238.sumPrice:2*238}, {num:4.price:213.sumPrice:4*213}]
// Process
const data=res.data.map(item= > { return {num:item.name,price:item.price,sumPrice:item.num * item.price} })
Copy the code
Scene 2:
- Description: Adds a status or subscript identifier to each item in the list
- Scene graph:
// Background data
res.data=[{...,status:0}, {... .status:1}]
// We need the data
data=[{...,status:0.isStudy:'To learn'}, {... .status:1.isStudy:'Done'}]
// Process
const data=res.data.map(item= > {
if(item.status) return{... item,isStudy:'Done'}
else return{... item,isStudy:'To learn'}})Copy the code
2. Show the details of the form and do data processing
- Scene graph:
/ / the background data | | click details to get the entire rows of data
record = {id:0.name:'Bobo can only Cv'. }// We need the data
data = [{title:'user ID'.text:0}, {title:'name'.text:'Bobo can only Cv'}... ]// Process
const list=[{title:'users'.text:record.id},{title:'name'.text:record.name}... ]Copy the code
3. Click the button to switch the picture up and down (click the button by default to get the index of the current picture)
let list = [......]
function getList(index){
// Copy the list first to prevent unnecessary bugs
let newList = list.map(item= > item)
// Define a variable to store information about the current data (image)
const tem = Object.assign(newList[index]);
// copy the assignment of the previous data to the current data
newList[index] = Object.assign(newList[index - 1])
// Assign tem to the previous data
newList[index - 1] = tem
/ / return newlist
return newList
}
Copy the code
4. Change the field name
// Background data
res.data = [{id:2.lable:'ssss'},... ]// We need the data
data = [{uid:2.msg:'ssss'},... ]// Process (shallow)
const data = res.data.map(item= > { return {uid:item.id,msg:item.lable} })
Copy the code