let sarr = [];
let tarr = [{
"code": "isCurr",
"name": "",
"width": 150
}, {
"code": "bussDate",
"name": "",
"width": 150
}, {
"code": "expiryDate",
"name": "",
"width": 150
}]
tarr.forEach((item)=>{
if(item.code === "isCurr"){
sarr.push({
name: "",
code: "curCodeData",
type: "select"
});
}
if(item.code=="bussDate"){
if(!sarr.includes({
name: "",
code: "bussDate",
type: "bussDatePicker"
})){
sarr.push({
name: "",
code: "bussDate",
type: "bussDatePicker"
});
}
}
if(item.code=="expiryDate"){
if(!sarr.includes({
name: "",
code: "expiryDate",
type: "expiryDatePicker"
})){
sarr.push({
name: "",
code: "expiryDate",
type: "expiryDatePicker"
});
}
}
if(item.code=="bussDate"||item.code=="expiryDate"){
console.log(sarr)
if(!sarr.includes({
name: "",
code: "summary",
type: "summaryinput"
})){
console.log("==$$$")
sarr.push({
name: "",
code: "summary",
type: "summaryinput"
});
}
}
})
console.log(srr)
as shown in the above code, loop an array of objects to get the final new array srr according to the internal conditions, but according to this logic, you always get one more summary as follows
0
:
{name: "", code: "curCodeData", type: "select"}
1
:
{name: "", code: "bussDate", type: "bussDatePicker"}
2
:
{name: "", code: "summary", type: "summaryinput"}
3
:
{name: "", code: "expiryDate", type: "expiryDatePicker"}
4
:
{name: "", code: "summary", type: "summaryinput"}
because the business requirement and this is written on someone else"s code, you can"t use an array to re-solve the problem.
The correct format ofps: requires that there is no third element with a summary, as long as the last element, that is, should be push in order
.