JavaScript කුකුලනි උදව් කරම්න

Rowdy Baby

Well-known member
  • Dec 22, 2018
    3,229
    4,470
    113
    Colombo
    මචන්ලා frontend form එකක ඩේටා බැකෙන්ඩ් එකට යවනකොට empty value null value undefined tiyena ewa ayne karala actual data tiyena tika witharak,array of object walin ayne kara ganne kohomada
    me wage data set ekak yanne
    JSON:
    {
      school: [
        {
          schoolname: 'ABC',
          sclfrom: '2001-12-12',
          sclto: '2007-07-07',
          special: 'Head Prefect',
          title: '',
          file: ''
        },
       {
          schoolname: 'london collage',
          sclfrom: '2001-12-12',
          sclto: '2007-07-07',
          special: 'Head Prefect',
          title: 'AL certificate',
          file: ''
        }
      ]
    }

    oke machan title:'' mehema tiyenawanam ee title kiyana eka ayne wenna oni ehema karanne khomada machanla.Nodejs backend ekak

    bump ekak wath dagena yanna sahola
     
    Last edited:

    HAneo

    Well-known member
  • Jan 30, 2007
    12,970
    29,168
    113
    Homagama
    Most basic thing you can do is this something like this.
    JavaScript:
    let  schools = [
        {
          schoolname: 'ABC',
          sclfrom: '2001-12-12',
          sclto: '2007-07-07',
          special: 'Head Prefect',
          title: '',
          file: ''
        },
       {
          schoolname: 'london collage',
          sclfrom: '2001-12-12',
          sclto: '2007-07-07',
          special: 'Head Prefect',
          title: 'AL certificate',
          file: ''
        }
      ];
      console.log(schools);
      schools.forEach(elm => {
          if(elm.title == ''){
              delete elm.title;
          }
      })
      console.log(schools);

    bus this would remove title from first object and keep the title in second cus it's not empty and inconsistence data being passed to the express controller

    so i think this is not the correct way to handle this. you must validate the body inside node js before bind it to a js object
    I assume you use express so check out express-validator. using that you can validate the passing json object before using it
     

    CorD SaC

    Well-known member
  • Feb 4, 2015
    15,762
    28,118
    113
    මේ දීලා තියෙන එවගෙන උබේ වැඩේ ගොඩ දාන්න බැරිද? එහෙනම් කියපන්කො
     

    peakySwifter

    Active member
  • May 27, 2021
    205
    150
    43
    JavaScript:
    schools.filter(school => !!school.title)
    Machan, this will remove the entire object instead of the "title" property.

    Try the following solution

    JavaScript:
     let aa = schools.map(({title,...restSchool})=>{
        if(title){
            return {title,...restSchool}
        }
        return restSchool
    })

    Here is a oneliner ;-)

    let bb = schools.map(({title,...rest})=>(title?{title,...rest}:rest));

    The new object looks like this.

    1645489446735.png


    Here also the two data objects are inconsistent. Check with your requirement
    ------ Post added on Feb 22, 2022 at 5:55 AM
     
    Last edited: