Js loop object array adding elements to a new array one by one

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 of

ps: requires that there is no third element with a summary, as long as the last element, that is, should be push in order

.
Aug.13,2021

var obj = {}
for (var iDeposit I < tarr.length;iPP) {

switch(tarr[i].code){
    case 'isCurr' :
        obj.select = {name: "", code: "curCodeData", type: "select"}
        break;
    case 'bussDate' : 
        obj.bussDatePicker = {name: "", code: "bussDate", type: "bussDatePicker"};
        break;
    case 'expiryDate' :
        obj.expiryDatePicker = {name: "", code: "expiryDate", type: "expiryDatePicker"};
    default : 
        obj.summaryinput = {name: "", code: "summary", type: "summaryinput"}
}

}
for (var key in obj) {

sarr.push(obj[key])

}
console.log (sarr)

I don't know if I understand it correctly.


visually observe that your sarr.includes ({}) is all false.
{} = {}, {} = = {} all returns false


Includes () with the wrong parameters, the object is of compound type, and the two objects can never be congruent, so false is always returned. You can find it with array.every (), and then judge


based on the code in the object.

because each object is a reference type, the address on the memory of the comparison, so you directly determine whether to include an object, the result will never be, because each object memory address is different, you can compare one of the properties of the object

arr.forEach(item=>{
    if(sarr.name!==''){
        //
    }
})

you can do the same

tarr.forEach(item=>{
   if(JSON.stringify(sarr).indexOf(JSON.stringify(option))==-1){
       arr.push(option); // 
   }
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3f47c-2c467.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3f47c-2c467.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?