0

This is my nuxt js code in one of the methods.

this.allCampusWithDeaprtments = campusData.map(
          (item, index) => {
              const schoolData = this.deptInsights.filter((row) => {
                  return row.unit === item.unit;
              }).map(row => {
                  // row.departmentData = row.departmentData || []; 
                  // row.departmentData.push(...this.depts.filter((d) => {
                  //     return d.sisSchoolId === row.sisSchoolId;
                  // }));
                  // return row;
                  const departmentData = this.depts.filter(
                  d => d.sisSchoolId === row.sisSchoolId
                );
                return departmentData
              });

              console.log('department', departmentData);
              // );
              item.id = `toggle-${index}`;
              return {
                  ...item,
                  schoolData,
                  // departmentData,
                  hasOerData: false
              };
          }
      );

departmentData is not returning any value. What could be the problem? i wand the data which matches the mentioned case in the code

1
  • 5
    Your (row) => {} function returns nothing, so the filtered list is empty. const departmentData is never used. Whatever departmentData is outside, it has nothing to do with the departmentData inside the filter, because it’s out of scope. Commented May 17, 2021 at 8:03

3 Answers 3

1

Continuing the reasoning of a colleague @sebastian-simon

Your constant is private in the scope. Therefore, it is not defined. Read this article.

Try it like this:

this.allCampusWithDeaprtments = campusData.map(
let departmentData = null

(item, index) => {
    const schoolData = this.deptInsights.filter((row) => {
        row.unit === item.unit
        departmentData = this.depts.filter((d) => {
            d.sisSchoolId === row.sisSchoolId;
            return departmentData
        })

    }
    );

    console.log('department', departmentData);
    // );
    item.id = `toggle-${index}`;
    return {
        ...item,
        schoolData,
        departmentData,
        hasOerData: false
    };
});
Sign up to request clarification or add additional context in comments.

4 Comments

throwing errors using above code, also i need to get schoolData and departmentData
Then log this.depts. I don't see your full project
schoolData is gone
I need to get schoolData and departmentData both, is there any way?
0

I think your problem lies here:

const departmentData = this.depts.filter((d) => {
            d.sisSchoolId === row.sisSchoolId;
            return departmentData // 
        })

The filter callback requires a boolean to be returned. You are checking a condition d.sisSchoolId === row.sisSchoolId; but not returning it, instead you are returning departmentData. So keeping that in mind, shouldn't your filter code be:

this.allCampusWithDeaprtments = campusData.map(
    (item, index) => {
        let departmentData = [];
        const schoolData = this.deptInsights.filter((row) => {
            departmentData.push(...this.depts.filter((d) => {
                return d.sisSchoolId === row.sisSchoolId;
            }));
            return row.unit === item.unit;

        });

        console.log('department', departmentData);
        // );
        item.id = `toggle-${index}`;
        return {
            ...item,
            schoolData,
            departmentData,
            hasOerData: false
        };
    }
);

EDIT new Structure

this.allCampusWithDeaprtments = campusData.map(
    (item, index) => {
        // let departmentData = [];
        const schoolData = this.deptInsights.filter((row) => {
            return row.unit === item.unit;
        }).map(row => {
            row.departmentData = row.departmentData || []; 
            row.departmentData.push(...this.depts.filter((d) => {
                return d.sisSchoolId === row.sisSchoolId;
            }));
            return row;
        });

        // console.log('department', departmentData);
        // );
        item.id = `toggle-${index}`;
        return {
            ...item,
            schoolData,
            // departmentData,
            hasOerData: false
        };
    }
);

17 Comments

tried above thing, still its not returning the value
Edited my code, declared deparmentData outside of callback scope. However, I am not sure what you want as output in departmentData. Will it be an array or will it just be matched for only one row?
Can you explain what you want in output? Do you want to associate departmentData of respective schools? Can there exists multiple depts for one school data?
Ii need to get schoolData and departmentData both as arrays, is there any way?
Can you explain what you want in output? YES Do you want to associate departmentData of respective schools? Can there exists multiple depts for one school data? YES
|
0

As per my knowledge, there can be 2 cases:-

  1. Your condition is not satisficed as per the data u pass in campusData array variable.
  2. you are not returning departmentData,
return const departmentData = this.depts.filter((d) => {...

Let me know, if its helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.