[英]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();
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
数组中返回元素。
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
数组,并使用arrDashboardIconGroups
的find
函数,并返回匹配的对象。
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.