Filter data processing after selected items in js tree array are selected

const listSaleSurvey = [
  {
    
    
    survey1Text: "胎压、刹车、电机是否正常",
    surveyId: 1,
    survey1List: [
      {
    
    
        id: "1",
        text: "正常",
        activeFlag: true,
      },
      {
    
    
        id: "2",
        text: "异常",
        activeFlag: false,
      },
    ],
  },
  {
    
    
    survey1Text: "充电频次",
    surveyId: 2,
    survey1List: [
      {
    
    
        id: "3",
        text: "每天一充",
        activeFlag: true,
      },
      {
    
    
        id: "4",
        text: "两到三天",
        activeFlag: false,
      },
      {
    
    
        id: "5",
        text: "四到五天",
        activeFlag: false,
      },
      {
    
    
        id: "6",
        text: "六到七天",
        activeFlag: false,
      },
      {
    
    
        id: "7",
        text: "七天以上",
        activeFlag: false,
      },
    ],
  },
  {
    
    
    survey1Text: "转绿灯后多久拔电源",
    surveyId: 3,
    survey1List: [
      {
    
    
        id: "8",
        text: "1.5小时",
        activeFlag: true,
      },
      {
    
    
        id: "9",
        text: "2小时",
        activeFlag: false,
      },
      {
    
    
        id: "10",
        text: "不清楚",
        activeFlag: false,
      },
    ],
  },
  {
    
    
    survey1Text: "车辆详情",
    surveyId: 4,
    survey1List: [
      {
    
    
        id: "11",
        text: "二轮车",
        activeFlag: true,
      },
      {
    
    
        id: "12",
        text: "三轮车",
        activeFlag: false,
      },
      {
    
    
        id: "13",
        text: "四轮车",
        activeFlag: false,
      },
    ],
  },
];

// 过滤出 activeFlag 为 true 的 survey1Text 和 survey1List,并移除 activeFlag 属性
const filteredSurveys = listSaleSurvey.reduce((result, survey) => {
    
    
  const filteredList = survey.survey1List.filter((item) => item.activeFlag);
  if (filteredList.length > 0) {
    
    
    result.push({
    
    
      survey1Text: survey.survey1Text,
      survey1List: filteredList.map(({
    
     activeFlag, ...rest }) => rest),
    });
  }
  return result;
}, []);

// 输出结果
console.log(filteredSurveys);

Guess you like

Origin blog.csdn.net/LRQQHM/article/details/132589616