gpt4 book ai didi

javascript - 从数组中获取和匹配值

转载 作者:行者123 更新时间:2023-12-02 18:34:08 25 4
gpt4 key购买 nike

我有一个像这样的数组:

var Data=[{
"Class":"item1","element":["a","b","c","d","e"],
"Class":"item2","element":["a","b","c","d","e"],
"Class":"item3","element":["a","b","c","d","e"],
"Class":"item4","element":["a","b","c","d","e"]
}];

我有两个变量:

var class='item1';
var element='a';

我想要一个条件,如果 var 类值匹配“item1”,则 var 元素值应该仅与其元素匹配,如下所示:

if (class == Data.Class) then 元素应该与其附近的 Data.element 匹配

有什么想法吗?

最佳答案

这应该是您的数据...

var Data= [
{ "Class":"item1","element":["a","b","c","d","e"] },
{ "Class":"item2","element":["a","b","c","d","e"] },
{ "Class":"item3","element":["a","b","c","d","e"] },
{ "Class":"item4","element":["a","b","c","d","e"] },
];

然后你可以迭代它并找到你的值。像这样

var class='item1';
var element='a';
for(var i = 0; i < Data.length; ++i){
if (Data[i].Class == class){
for( var j = 0; j < Data[i].element.length; ++j){
if ( Data[i].elements[j] == element){
// do whatever
}
}
}
}

关于javascript - 从数组中获取和匹配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com