从数组中获取和匹配值

时间:2022-12-29 20:13:39

I have an array which is like this:

我有一个这样的数组:

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"]
}];

I have two variables:

我有两个变量:

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

I want a condition that if var class values match "item1" then var element value should be matched with its elements only like:

我想要一个条件,如果var类值匹配“item1”,那么var元素值应该只与其元素匹配,如:

if (class == Data.Class) then element should be matched with Data.element near it

如果(class == Data.Class)那么元素应该与它附近的Data.element匹配

Any ideas?

2 个解决方案

#1


2  

This should be your data...

这应该是你的数据......

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"] },
];

Then you can iterate over it and find your value. Like this

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

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
            }
        }
    }
}

#2


0  

First of all you need to fix your data

首先,您需要修复数据

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"] },
];

Now it is an array of objects.

现在它是一个对象数组。

Then you can look into that array, for example with a for loop

然后你可以查看那个数组,例如使用for循环

var myclass = "item3"; // class is a reserved word
for (var i=0; i<Data.length;i++) {
    if (Data[i].Class == myclass) {
        // do something with that its data Data[i].element
    }
}

#1


2  

This should be your data...

这应该是你的数据......

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"] },
];

Then you can iterate over it and find your value. Like this

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

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
            }
        }
    }
}

#2


0  

First of all you need to fix your data

首先,您需要修复数据

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"] },
];

Now it is an array of objects.

现在它是一个对象数组。

Then you can look into that array, for example with a for loop

然后你可以查看那个数组,例如使用for循环

var myclass = "item3"; // class is a reserved word
for (var i=0; i<Data.length;i++) {
    if (Data[i].Class == myclass) {
        // do something with that its data Data[i].element
    }
}