[英]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
.我想通过
contact
过滤这个array of objects
。 My input is let searchByContact = 7097891779;
我的输入是
let searchByContact = 7097891779;
. . I tried but nothing appeared on console (not Output nor Error).
我尝试过,但控制台上没有出现任何内容(不是 Output 也不是错误)。 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对于上述
input
,我预期的 output 应该像
How can I get this?我怎样才能得到这个? Help,!
帮助,! Also, I want set same filter for
session
in my next case.另外,我想在下一个案例中为
session
设置相同的过滤器。 Kindly let me know that value
of session
is a valid input format or not?请让我知道
session
的value
是否是有效的输入格式?
You have a data type problem.您有数据类型问题。
Make a string data type of contact and session, for example:制作一个字符串数据类型为contact和session,例如:
{ 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") );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.