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
  • 477
  • 1
  • 7
  • 18

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
  • 376,160
  • 25
  • 347
  • 392
  • 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
  • 16,452
  • 1
  • 18
  • 39
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
  • 5,292
  • 1
  • 10
  • 20
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
  • 1,011
  • 2
  • 11
  • 27