简体   繁体   中英

Trying to Get Value(s) from Array of Objects in JavaScript

I have an array of objects

let student = [
      {
          rollNo: 1, name: "Rohit", fatherName: "Lalit", motherName: "Abha", age: 22, contact: 7097991361, session: 2017-2021,
          institute: "college", course: "MBBS", stream: "dental"
      },
      {
          rollNo: 2, name: "Rohan", fatherName: "Ram", motherName: "Akanksha", age: 23, contact: 7097775553, session: 2017-2019,
          institute: "college", course: "MA", stream: "history"
      },
      {
          rollNo: 3, name: "Ishfaq", fatherName: "Maqbool", motherName: "Amala", age: 24, contact: 7097891779, session: 2016-2020,
          institute: "college", course: "bTech", stream: "civil"
      },
      {
          rollNo: 4, name: "Sanjay", fatherName: "Ramesh", motherName: "Aditi", age: 24, contact: 7097683032, session: 2017-2018,
          institute: "school", class: 12, stream: "medical"
      },
      {
          rollNo: 5, name: "Anuj", fatherName: "Narayan", motherName: "Amita", age: 23, contact: 7097574082, session: 2018-2019,
          institute: "school", class: 11, stream: "nonMedical"
      }
];

I want to filter this array of objects by contact . My input is let searchByContact = 7097891779; . I tried but nothing appeared on console (not Output nor Error). Here's my code:

function fetchStuByCntct() {
    let StuByCntct = filteredArr.filter(obj => obj["contact"] === searchByContact);
    return StuByCntct;
}
let getStuByCntct = fetchStuByCntct();

if ((searchByRollNo === 0) && (searchByName === "") && (searchByFthrName === "") && (searchByMthrName === "") 
    && (searchByInstitute === "") && (searchByMinAge === 0) && (searchByMaxAge === 0) 
    && (searchByContact === searchByContact) && (searchBySession === 0) && (searchByClass === "") && (searchByCourse === "") 
    && (searchByStream === "") && (searchByAlphabet === "")) {
    console.log(getStuByCntct);
}

For an input mentioned above, My expected output should be like

Output:
在此处输入图像描述

How can I get this? Help,! Also, I want set same filter for session in my next case. Kindly let me know that value of session is a valid input format or not?

You have a data type problem.

Make a string data type of contact and session, for example:

 {
    rollNo: 2, 
    name: "Rohan",
    fatherName: "Ram",
    motherName: "Akanksha",
    age: 23,
    contact: "7097775553",
    session: "2017-2019",
    institute: "college",
    course: "MA",
    stream: "history"
}

Then the filter will work as it should.

 let student = [ { rollNo: 1, name: "Rohit", fatherName: "Lalit", motherName: "Abha", age: 22, contact: "7097991361", session: "2017-2021", institute: "college", course: "MBBS", stream: "dental" }, { rollNo: 2, name: "Rohan", fatherName: "Ram", motherName: "Akanksha", age: 23, contact: "7097775553", session: "2017-2019", institute: "college", course: "MA", stream: "history" }, { rollNo: 3, name: "Ishfaq", fatherName: "Maqbool", motherName: "Amala", age: 24, contact: "7097891779", session: "2016-2020", institute: "college", course: "bTech", stream: "civil" }, { rollNo: 4, name: "Sanjay", fatherName: "Ramesh", motherName: "Aditi", age: 24, contact: "7097683032", session: "2017-2018", institute: "school", class: 12, stream: "medical" }, { rollNo: 5, name: "Anuj", fatherName: "Narayan", motherName: "Amita", age: 23, contact: "7097574082", session: "2018-2019", institute: "school", class: 11, stream: "nonMedical" } ]; const search = (name, value) => { return student.filter( obj => obj[name] === value); } console.log( "contact 7097891779:", search("contact", "7097891779") ); console.log( "session 2017-2018:", search("session", "2017-2018") );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM