0
const absentStudentsId = [78, 15, 41, 30] // ======> [{stdId: 78, isPresent: false}, {stdId: 15, isPresent: false}, {stdId: 41, isPresent: false}, {stdId: 30, isPresent: false}]

const presentStudentsId = [80, 61] // ======> [{stdId: 80, isPresent: true}, {stdId: 61, isPresent: true}]

const students = [
  { stdId: 78, isPresent: false },
  { stdId: 15, isPresent: false },
  { stdId: 41, isPresent: false },
  { stdId: 30, isPresent: false },
  { stdId: 80, isPresent: true },
  { stdId: 61, isPresent: true },
]

I want to implement that logic as you see in the commented line.

Ahmed Ibrahim
  • 373
  • 1
  • 3
  • 14

4 Answers4

8

You could take a closure over isPresent and map new objects.

const
    buildObject = isPresent => stdId => ({ stdId, isPresent }),
    absentStudentsId = [78, 15, 41, 30],
    presentStudentsId = [80, 61],
    students = [
        ...absentStudentsId.map(buildObject(false)),
        ...presentStudentsId.map(buildObject(true))
    ];
    
console.log(students);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
  • Great answer, can you explain `buildObject = isPresent => stdId => ({ stdId, isPresent }),` this line? I am guessing `isPresent` is the `false` or `true` you passed inside `buildObject` and `stdId` is passed by the map function itself? – Sinan Yaman Jun 21 '21 at 08:53
  • 4
    @SinanYaman correct. [It's a curried function](https://stackoverflow.com/questions/32782922/what-do-multiple-arrow-functions-mean-in-javascript) – VLAZ Jun 21 '21 at 08:56
1

.map() -> loop over array elements and return a new item for eachitem with your desired computation.

...(Spread) -> spread operator which expands an array into individual elements.

let ans = [...(absentStudentsId.map(x => { return { stdId : x, present : false} })),...(presentStudentsId.map(x => {return { stdId : x, present : true} }))]
Tushar Shahi
  • 10,769
  • 1
  • 9
  • 29
1

const absentStudentsId = [78, 15, 41, 30] // ======> [{stdId: 78, isPresent: false}, {stdId: 15, isPresent: false}, {stdId: 30, isPresent: false}]

const presentStudentsId = [80, 61] // ======> [{stdId: 80, isPresent: true}, {stdId: 61, isPresent: true}]

const transformStudents = (students, isPresent) => students.map(studentId => ({ stdId: studentId, isPresent}));

const allStudents = [...transformStudents(absentStudentsId, false), ...transformStudents(presentStudentsId, true)];

console.log(allStudents)
Shyam
  • 4,754
  • 1
  • 8
  • 19
1

Yet another straightforward solution:

const absentStudentsId = [78, 15, 41, 30]
const presentStudentsId = [80, 61]

const students = []
absentStudentsId.forEach(id => students.push({stdID: id, isPresent: false}))
presentStudentsId.forEach(id => students.push({stdID: id, isPresent: true}))

console.log(students)
/*
[
  { stdID: 78, isPresent: false },
  { stdID: 15, isPresent: false },
  { stdID: 41, isPresent: false },
  { stdID: 30, isPresent: false },
  { stdID: 80, isPresent: true },
  { stdID: 61, isPresent: true }
]
*/
jboockmann
  • 925
  • 2
  • 10
  • 24