简体   繁体   English

查找方法不返回“第 0 个”匹配对象反应,或反应原生或 javascript

[英]find method not returning '0th' matched object react, or react-native or javascript

Not getting all matched objects, its is returning all objects except '{ ID:0, Name: 'General Group' }', i want all matched objects没有得到所有匹配的对象,它正在返回除“{ ID:0,名称:‘通用组’}”之外的所有对象,我想要所有匹配的对象

 let arrDashboardIconGroups = [ { ID:0, Name: 'General Group' }, { ID: 1, Name: 'Patient Administration' }, { ID: 2, Name: 'Medical Charts' }, { ID: 3, Name: 'Medical Procedures' }, { ID: 13, Name: 'Purchase' }, { ID: 14, Name: 'Sales' }, { ID: 5, Name: 'Insurance' }, { ID: 4, Name: 'Cash' }, { ID: 6, Name: 'Pharmacy' }, { ID: 7, Name: 'Inventory' }, { ID: 8, Name: 'Lab' }, { ID: 9, Name: 'Imaging' }, { ID: 10, Name: 'In Patient' }, { ID: 11, Name: 'System Administration' }, { ID: 12, Name: 'Accounting' } ] const getMatchedobjects=()=> { let result = [0,1,2,3,4,] let matchedArray = arrDashboardIconGroups.filter(val =>result.find( (items)=>items == val.ID)) console.log(matchedArray) } getMatchedobjects()

You are returning the value of你正在返回值

result.find((items) => items == val.ID)

In the first case, the value returned is 0 which is a falsy value.在第一种情况下,返回的值为0 ,这是一个falsy值。 So It won't include in the final filter result.所以它不会包含在最终的过滤结果中。

You can run the below code and see the returning values.您可以运行以下代码并查看返回值。

 let arrDashboardIconGroups = [ { ID: 0, Name: "General Group" }, { ID: 1, Name: "Patient Administration" }, { ID: 2, Name: "Medical Charts" }, { ID: 3, Name: "Medical Procedures" }, { ID: 13, Name: "Purchase" }, { ID: 14, Name: "Sales" }, { ID: 5, Name: "Insurance" }, { ID: 4, Name: "Cash" }, { ID: 6, Name: "Pharmacy" }, { ID: 7, Name: "Inventory" }, { ID: 8, Name: "Lab" }, { ID: 9, Name: "Imaging" }, { ID: 10, Name: "In Patient" }, { ID: 11, Name: "System Administration" }, { ID: 12, Name: "Accounting" }, ]; const getMatchedobjects = () => { let result = [0, 1, 2, 3, 4]; let matchedArray = arrDashboardIconGroups.filter((val) => { const returnResult = result.find((items) => items == val.ID); console.log(returnResult); return returnResult; // result.includes(val.ID) }); console.log(matchedArray); }; getMatchedobjects();

Alternatively, you can use includes或者,您可以使用包含

 let arrDashboardIconGroups = [ { ID: 0, Name: "General Group" }, { ID: 1, Name: "Patient Administration" }, { ID: 2, Name: "Medical Charts" }, { ID: 3, Name: "Medical Procedures" }, { ID: 13, Name: "Purchase" }, { ID: 14, Name: "Sales" }, { ID: 5, Name: "Insurance" }, { ID: 4, Name: "Cash" }, { ID: 6, Name: "Pharmacy" }, { ID: 7, Name: "Inventory" }, { ID: 8, Name: "Lab" }, { ID: 9, Name: "Imaging" }, { ID: 10, Name: "In Patient" }, { ID: 11, Name: "System Administration" }, { ID: 12, Name: "Accounting" }, ]; const getMatchedobjects = () => { let result = [0, 1, 2, 3, 4]; let matchedArray = arrDashboardIconGroups.filter((val) => result.includes(val.ID)); console.log(matchedArray); }; getMatchedobjects();

Issue问题

Find returns a value/object (the first matching), but it seems you want to filter the arrDashboardIconGroups array by those that match an id specified in the result array. Find 返回一个值/对象(第一个匹配项),但您似乎希望通过与result数组中指定的id匹配的那些来过滤arrDashboardIconGroups数组。 When result[0] is returned, 0 is the value, which is falsey, and the filter doesn't return the element from the arrDashboardIconGroups array.当返回result[0]0是值,这是 falsey,并且过滤器不会从arrDashboardIconGroups数组中返回元素。

Solution解决方案

Use Array.prototype.some to return the boolean that filter needs to include an element in the result array.使用Array.prototype.some返回filter需要在结果数组中包含元素的布尔值。

 let arrDashboardIconGroups = [ { ID:0, Name: 'General Group' }, { ID: 1, Name: 'Patient Administration' }, { ID: 2, Name: 'Medical Charts' }, { ID: 3, Name: 'Medical Procedures' }, { ID: 13, Name: 'Purchase' }, { ID: 14, Name: 'Sales' }, { ID: 5, Name: 'Insurance' }, { ID: 4, Name: 'Cash' }, { ID: 6, Name: 'Pharmacy' }, { ID: 7, Name: 'Inventory' }, { ID: 8, Name: 'Lab' }, { ID: 9, Name: 'Imaging' }, { ID: 10, Name: 'In Patient' }, { ID: 11, Name: 'System Administration' }, { ID: 12, Name: 'Accounting' } ]; const getMatchedobjects = () => { let result = [0,1,2,3,4,]; let matchedArray = arrDashboardIconGroups.filter(val => result.some((items)=>items == val.ID)); console.log(matchedArray); } getMatchedobjects();

Alternatively you could also check that result array includes the matching id .或者,您还可以检查result数组是否包含匹配的id

 let arrDashboardIconGroups = [ { ID:0, Name: 'General Group' }, { ID: 1, Name: 'Patient Administration' }, { ID: 2, Name: 'Medical Charts' }, { ID: 3, Name: 'Medical Procedures' }, { ID: 13, Name: 'Purchase' }, { ID: 14, Name: 'Sales' }, { ID: 5, Name: 'Insurance' }, { ID: 4, Name: 'Cash' }, { ID: 6, Name: 'Pharmacy' }, { ID: 7, Name: 'Inventory' }, { ID: 8, Name: 'Lab' }, { ID: 9, Name: 'Imaging' }, { ID: 10, Name: 'In Patient' }, { ID: 11, Name: 'System Administration' }, { ID: 12, Name: 'Accounting' } ]; const getMatchedobjects=()=> { let result = [0,1,2,3,4,]; let matchedArray = arrDashboardIconGroups.filter(val => result.includes(val.ID)) ; console.log(matchedArray); } getMatchedobjects();

find() returns value of the first element in the provided array that satisfies the provided testing function. find()返回提供的数组中满足提供的测试函数的第一个元素的值。 Since 0 is a falsy value, it does not include it in the final array.由于0是一个假值,因此它不包括在最终数组中。 See MDN Docs 查看 MDN 文档

You can map the result array, and use arrDashboardIconGroups 's find function, and return the matching objects.您可以map result数组,并使用arrDashboardIconGroupsfind函数,并返回匹配的对象。

 let arrDashboardIconGroups = [ { ID: 0, Name: 'General Group' }, { ID: 1, Name: 'Patient Administration' }, { ID: 2, Name: 'Medical Charts' }, { ID: 3, Name: 'Medical Procedures' }, { ID: 13, Name: 'Purchase' }, { ID: 14, Name: 'Sales' }, { ID: 5, Name: 'Insurance' }, { ID: 4, Name: 'Cash' }, { ID: 6, Name: 'Pharmacy' }, { ID: 7, Name: 'Inventory' }, { ID: 8, Name: 'Lab' }, { ID: 9, Name: 'Imaging' }, { ID: 10, Name: 'In Patient' }, { ID: 11, Name: 'System Administration' }, { ID: 12, Name: 'Accounting' } ] const getMatchedobjects=()=> { let result = [0, 1,2,3,4,99] //99 for trial let matchedArray = result.map((res)=>arrDashboardIconGroups.find((val)=>val.ID == res)).filter(Boolean); console.log(matchedArray) } getMatchedobjects()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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