2

I have an object that looks like this:

let res = [{
  stop: "stop1",
  students: [
     "Maria",
     "Mario"
  ]},
 {stop: "stop2",
 students: [
   "Giovanni",
   "Giacomo"
 ]
}];

and a function that checks if a student is already present at a given bus stop:

checkStudent(stopName: string, studentName: string): boolean {
   // iterate over res and check if students is present
}

what I've done so far is iterate over res object, check every stopName until one of these match with the 'stopName' parameter and then iterate over students array to check if student is present. I'd like to know if there is a better way to do this. Can I directly access the right students array given the stop name? I'm using typescript

4
  • 1
    Please share checkStudent function content Commented May 19, 2019 at 8:09
  • 1
    Your res object does not look normal. Does it really have 2 keys with the same name students and stop ? Commented May 19, 2019 at 8:12
  • ops it's an array I'll edit my question Commented May 19, 2019 at 8:14
  • Can I directly access the right students array given the stop name? no, loops are needed. But you can use standard functions that do this loop for you, like find() and some(). developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 19, 2019 at 8:21

2 Answers 2

5

First thing your res object is declare incorrectly, It should be array as below code example.

And to check your constraint you can use some to and includes as below example.

If you want object then use filter instead of some.

let res = [{
  stop: "stop1",
  students: [
    "Maria",
    "Mario"
  ]
}, {
  stop: "stop2",
  students: [
    "Giovanni",
    "Giacomo"
  ]
}];

function checkStudent(stopName, studentName) {
  return res.some(x => x.stop == stopName && x.students.includes(studentName));
}

function checkStudentAndReturnObject(stopName, studentName) {
  return res.filter(x => x.stop == stopName && x.students.includes(studentName));
}

console.log(checkStudent("stop1", "Maria"));
console.log(checkStudentAndReturnObject("stop1", "Maria"));

Sign up to request clarification or add additional context in comments.

3 Comments

Is it better to forbid occurrences of the same element by return res.filter(x => x.stop == stopName && x.students.includes(studentName))[0];
@Abdulrahman, we shouldn’t do that because if no matching object found then it will return undefined. Rather then empty array.
thanks, that was exactly what I was looking for. I'll check the differences between some and filter :)
0

You can't really "directly" access the right students array given the stop name because the array is not keyed by the stop name, however you can do this to save you using a for loop:

const targetStop = res.find(stopObject => stopObject.stop === stopName);

targetStop.students // <--- your array

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.