我想要获取我集合中指定的元素并与之进行匹配验证
代码如下
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'))
用find方法吧
【以下回答由 GPT 生成】
首先,我们需要明确一下问题,您想要通过哪个属性来获取指定的数组集合元素进行比较验证?给出一个属性值,然后我们将在代码中展示如何实现。
假设您想要通过SolutionNo属性来获取指定的数组集合元素进行比较验证,下面是一个示例代码来实现这个功能:
// 假设您有一个名为basicMeta的数组集合
const basicMeta = [
{
SolutionNo: "壹号方案",
attributes: "DescAttributes",
// 其它属性...
},
{
SolutionNo: "贰号方案",
attributes: "DescAttributes",
// 其它属性...
},
// 其它元素...
];
// 假设您有一个名为formattribute的对象,其中包含SolutionNo属性
const formattribute = {
SolutionNo: "壹号方案",
// 其它属性...
};
// 使用Array.find()方法根据SolutionNo属性值查找指定的元素
const currentCollection = basicMeta.find(item => item.SolutionNo === formattribute.SolutionNo);
if (currentCollection) {
// 找到了匹配的元素
console.log("找到匹配的元素:", currentCollection);
} else {
// 没有找到匹配的元素
console.log("未找到匹配的元素");
}
以上代码使用了Array.find()
方法来查找数组集合中满足条件的第一个元素。在回调函数中,我们使用item.SolutionNo === formattribute.SolutionNo
来判断元素的SolutionNo属性是否和指定的值相等。
如果找到了匹配的元素,currentCollection
变量将存储该元素。您可以根据实际需求来处理这个元素。如果没有找到匹配的元素,currentCollection
变量将为undefined
。
请注意,在使用这段代码之前,您需要将basicMeta和formattribute替换为您实际的数据。
希望这个解决方案对您有帮助!如果您有任何其他问题,请随时提问。
【相关推荐】