![](/img/trans.png)
[英]javascript How to get an objects value from an array full of objects?
[英]Trying to Get Value(s) from Array of Objects in JavaScript
我有一個對象數組
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"
}
];
我想通過contact
過濾這個array of objects
。 我的輸入是let searchByContact = 7097891779;
. 我嘗試過,但控制台上沒有出現任何內容(不是 Output 也不是錯誤)。 這是我的代碼:
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);
}
對於上述input
,我預期的 output 應該像
我怎樣才能得到這個? 幫助,! 另外,我想在下一個案例中為session
設置相同的過濾器。 請讓我知道session
的value
是否是有效的輸入格式?
您有數據類型問題。
制作一個字符串數據類型為contact和session,例如:
{ rollNo: 2, name: "Rohan", fatherName: "Ram", motherName: "Akanksha", age: 23, contact: "7097775553", session: "2017-2019", institute: "college", course: "MA", stream: "history" }
然后過濾器將正常工作。
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") );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.