Home > Software engineering >  How can I return newly declared variable that is appended to the object in map function
How can I return newly declared variable that is appended to the object in map function

Time:08-16

Check the code below

const responseData = {
    students: [
        {
            id: 1,
            classAttended: 'Level 4',
            classRoomNo: '101',
            attendance: '3',
            marks: '50',
            optIn: 'Y',
            historyDataList: [
                {
                    id: 6,
                    shivirdetails: {
                        id: 1,
                        year: '2016',
                    },
                    classAttended: 'Level 1',
                    classRoomNo: '108',
                    attendance: '5',
                    marks: '50',
                    optIn: 'Y',
                },
            ],
        },
        {
            id: 2,
            classAttended: 'Level 3',
            classRoomNo: '101',
            attendance: '3',
            marks: '50',
            optIn: 'Y',
            historyDataList: [
                {
                    id: 8,
                    shivirdetails: {
                        id: 6,
                        year: '2016',
                    },
                    classAttended: 'Level 3',
                    classRoomNo: '101',
                    attendance: '5',
                    marks: '64',
                    optIn: 'Y',
                },
            ],
        },
    ],
};

const getHistoricalData = (student, year, key) => {
    const { historyDataList } = student;
    const res = historyDataList.map((historyData) => {
        const {
            shivirdetails,
            shivirdetails: { year: shivirYear},
        } = historyData;
        if (shivirdetails && shivirYear == year && historyData[key]) {
            return historyData[key];
        }
    });
    return res[0]
};

const {students} = responseData

const result = students.map((student) => {
  student.classRoomNo2016 = getHistoricalData(student, 2016, 'classRoomNo')
  student.marks2016 = getHistoricalData(student, 2016, 'marks')
  student.classAttended2016 = getHistoricalData(student, 2016, 'classAttended')
})

console.log(result)

The result yields undefined. Probably it is because nothing is returned in the result's mapped function. I want to know how can I return the student and those values appended to it. If I console those values they give me proper data. But I am unable to return it that's why my result shows array of two undefined.

CodePudding user response:

Your map function is just setting the value in student object but not returning anything. You can use student object along with spread operator to modify and add additional property and return as a response object in map function. Something like this :

    const result = students.map((student) => ({...student, 
      'classRoomNo2016' : getHistoricalData(student, 2016, 'classRoomNo'),
      'marks2016' : getHistoricalData(student, 2016, 'marks'),
      'classAttended2016' : getHistoricalData(student, 2016, 'classAttended')
    }))

const responseData = {
    students: [
        {
            id: 1,
            classAttended: 'Level 4',
            classRoomNo: '101',
            attendance: '3',
            marks: '50',
            optIn: 'Y',
            historyDataList: [
                {
                    id: 6,
                    shivirdetails: {
                        id: 1,
                        year: '2016',
                    },
                    classAttended: 'Level 1',
                    classRoomNo: '108',
                    attendance: '5',
                    marks: '50',
                    optIn: 'Y',
                },
            ],
        },
        {
            id: 2,
            classAttended: 'Level 3',
            classRoomNo: '101',
            attendance: '3',
            marks: '50',
            optIn: 'Y',
            historyDataList: [
                {
                    id: 8,
                    shivirdetails: {
                        id: 6,
                        year: '2016',
                    },
                    classAttended: 'Level 3',
                    classRoomNo: '101',
                    attendance: '5',
                    marks: '64',
                    optIn: 'Y',
                },
            ],
        },
    ],
};

const getHistoricalData = (student, year, key) => {
    const { historyDataList } = student;
    const res = historyDataList.map((historyData) => {
        const {
            shivirdetails,
            shivirdetails: { year: shivirYear},
        } = historyData;
        if (shivirdetails && shivirYear == year && historyData[key]) {
            return historyData[key];
        }
    });
    return res[0]
};

const {students} = responseData

const result = students.map((student) => ({...student, 
  'classRoomNo2016' : getHistoricalData(student, 2016, 'classRoomNo'),
  'marks2016' : getHistoricalData(student, 2016, 'marks'),
  'classAttended2016' : getHistoricalData(student, 2016, 'classAttended')
}))

console.log(result)

CodePudding user response:

In your map function, you don't have any return statement so it returns nothing. There are cases where you can return without a return statement.

For example

const result2 = students.map((student) => getHistoricalData(student, 2016, 'classRoomNo'));

The result is ["108", "101"]. This is actually a short version of this

    const result2 = students.map((student) => { return getHistoricalData(student, 2016, 'classRoomNo')});

So you cannot return with a return statement.

In your case, as I understand you want to return an object which should have 3 fields (classRoomNo2016, marks2016, classAttended2016). Thus you have to create an object to return.

It should look like something like this:

const result = students.map((student) => {
   student.classRoomNo2016 = getHistoricalData(student, 2016, 'classRoomNo');
   student.marks2016 = getHistoricalData(student, 2016, 'marks');
   student.classAttended2016 = getHistoricalData(student, 2016, 'classAttended');
   const returnType = {classRoomNo2016:student.classRoomNo2016,   marks2016:student.marks2016, classAttended2016:student.classAttended};
   return returnType;
 });

CodePudding user response:

If the value does not append, you can copy any object with JSON.parse(JSON.stringify());

In addition, inside the function getHistoricalData you return nothing if the condition does not match. You can simply return undefined.

You can find, investigate and run the fully fixed code in the snipped.

const responseData = {
    students: [
        {
            id: 1,
            classAttended: 'Level 4',
            classRoomNo: '101',
            attendance: '3',
            marks: '50',
            optIn: 'Y',
            historyDataList: [
                {
                    id: 6,
                    shivirdetails: {
                        id: 1,
                        year: '2016',
                    },
                    classAttended: 'Level 1',
                    classRoomNo: '108',
                    attendance: '5',
                    marks: '50',
                    optIn: 'Y',
                },
            ],
        },
        {
            id: 2,
            classAttended: 'Level 3',
            classRoomNo: '101',
            attendance: '3',
            marks: '50',
            optIn: 'Y',
            historyDataList: [
                {
                    id: 8,
                    shivirdetails: {
                        id: 6,
                        year: '2016',
                    },
                    classAttended: 'Level 3',
                    classRoomNo: '101',
                    attendance: '5',
                    marks: '64',
                    optIn: 'Y',
                },
            ],
        },
    ],
};

const getHistoricalData = (student, year, key) => {
    const { historyDataList } = student;
    const res = historyDataList.map((historyData) => {
        const {
            shivirdetails,
            shivirdetails: { year: shivirYear},
        } = historyData;
        if (shivirdetails && shivirYear == year && historyData[key]) {
            return historyData[key];
        } else {
            return undefined;
        }
    });
    return res[0]
};

const {students} = responseData;

const result = students.map((student) => {
  const cpStd = JSON.parse(JSON.stringify(student));
  cpStd["classRoomNo2016"] = getHistoricalData(student, 2016, 'classRoomNo');
  cpStd["marks2016"] = getHistoricalData(student, 2016, 'marks');
  cpStd["classAttended2016"] = getHistoricalData(student, 2016, 'classAttended');
  return cpStd;
})

console.log(result)

  • Related