Skip to content
Advertisement

How to change new Index number if value changes in Map

in Javascript

const qstndata = Object.entries(user.qstn).map(([key, values], index) => ({
      question_id: values._id,
      qs_no: index + 1,
      in_state: 0,
      section_index:    // here need to start with 1 if section_name changes
      section_name: values.sectionname,
    }));

if here values.sectionname changes New Index number needed how i can achieve this section_index

Advertisement

Answer

  let myindex = 0;
  let secname ="";
    const qstndata = Object.entries(user.qstn).map(([key, values], index) => ({
      
      qs_no: index + 1,
      sb_no :secname == values.sectionname?'': myindex =0 ,
      ss_no: myindex = myindex+1,
      secname :secname = values.sectionname ,
      
      section_name: values.sectionname,
    }));
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement