自在猫先生 2023-08-29 11:49 采纳率: 62.9%
浏览 20

React18.2获取数组集合中指定的值并进行比较

我想要获取我集合中指定的元素并与之进行匹配验证
代码如下

const currentCollection = basicMeta[0].SolutionNo=== formattribute.SolutionNo;

basicMeta数据结构:
0
: 
壹号方案  ///需要注意的是数组里面的这元素是动态的跟集合里面的这个SolutionNo一样
: 
Array(1)
0: 
SolutionNo: "壹号方案"
attributes: "DescAttributes"
en_name: "EnglishName"
fields: "PushDesc"
id: "13559775-c56c-4878-a50f-08db77bbab13"
key: "13559775-c56c-4878-a50f-08db77bbab13"
length: "1000"
name: "描述"
table_name: "PushName"
type: "nvarchar"
zh_name: "Chinese"



当数组对象有多个的时候为什么取不到集合里面的SolutionNo值 
const currentCollection = basicMeta[0][formattribute.SolutionNo].find(item => item.SolutionNo === formattribute.SolutionNo);

basicMeta数据结构:
第一个数组对象
0
: 
壹号方案  ///需要注意的是数组里面的这元素是动态的跟集合里面的这个SolutionNo一样
: 
Array(1)
0: 
SolutionNo: "壹号方案"
attributes: "DescAttributes"
en_name: "EnglishName"
fields: "PushDesc"
id: "13559775-c56c-4878-a50f-08db77bbab13"
key: "13559775-c56c-4878-a50f-08db77bbab13"
length: "1000"
name: "描述"
table_name: "PushName"
type: "nvarchar"
zh_name: "Chinese"

第二个数组对象
0
: 
1  ///需要注意的是数组里面的这元素是动态的跟集合里面的这个SolutionNo一样
: 
Array(1)
0: 
SolutionNo: "1"
attributes: "DescAttributes"
en_name: "EnglishName"
fields: "PushDesc"
id: "13559775-c56c-4878-a50f-08db77bbab13"
key: "13559775-c56c-4878-a50f-08db77bbab13"
length: "1000"
name: "描述"
table_name: "PushName"
type: "nvarchar"
zh_name: "Chinese"

第三个数组对象
0
: 
2 ///需要注意的是数组里面的这元素是动态的跟集合里面的这个SolutionNo一样
: 
Array(1)
0: 
SolutionNo: "2"
attributes: "DescAttributes"
en_name: "EnglishName"
fields: "PushDesc"
id: "13559775-c56c-4878-a50f-08db77bbab13"
key: "13559775-c56c-4878-a50f-08db77bbab13"
length: "1000"
name: "描述"
table_name: "PushName"
type: "nvarchar"
zh_name: "Chinese"

报错了:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'find'))

img

img

img

  • 写回答

2条回答 默认 最新

  • 一位热心程序猿 2023-08-29 13:05
    关注

    用find方法吧

    评论

报告相同问题?

问题事件

  • 修改了问题 8月29日
  • 修改了问题 8月29日
  • 修改了问题 8月29日
  • 修改了问题 8月29日
  • 展开全部