[英]Check if value exist in array
I would like to check if values in Array1 exist in Array2. 我想检查Array2中是否存在Array1中的值。 If it exist, then add the index for that value in a new array as a value. 如果存在,则在新数组中将该值的索引添加为一个值。
My solution below is doing almost that but it dosent alert me every same number, duplicates. 我下面的解决方案几乎可以做到这一点,但它会提醒我每个相同的数字,重复。 For exampel the value 4355 in array1, its 3 of that value, but it only alert me 1 time. 例如,将array1中的4355值,即该值的3,但仅提醒我1次。 I would like to alert me every time. 我想每次提醒我。
var array1 = [];
array1.push(3545);
array1.push(4355);
array1.push(5435);
array1.push(3457);
array1.push(4355);
array1.push(4355);
var array2 = [];
array2.push(1022);
array2.push(4355);
array2.push(2333);
array2.push(5656);
array2.push(1234);
array2.push(5555);
array2.push(7777);
array2.push(3455);
for(var i=0; i<array2.length; i++){
checkIfExist(array2[i], array1);
}
function checkIfExist(searchValue, searchArray){
if ($.inArray(searchValue, searchArray) < 0) {
alert(searchValue + " not found");
}
else {
alert(searchValue + " found with index(" + $.inArray(searchValue, array2) + ") in array");
}
}
To find all the indices of a particular value in an array you could use jQuery.each()
: 要查找数组中特定值的所有索引,可以使用jQuery.each()
:
var indices = [];
$.each(searchArray, function(index, value) {
if (value == searchValue) {
indices.push(index);
}
});
Now, indices
is an array with the indices at which the value was found, or empty if there were no matches. 现在, indices
是一个数组,其中包含在其上找到值的索引;如果没有匹配项,则为空。
I'm not sure to well understand what you want... hope it will help you! 我不确定您想了解什么,希望它能对您有所帮助!
var array1 = []; array1.push(3545); array1.push(4355); array1.push(5435); array1.push(3457); array1.push(4355); array1.push(4355); var array2 = []; array2.push(1022); array2.push(4355); array2.push(2333); array2.push(5656); array2.push(1234); array2.push(5555); array2.push(7777); array2.push(3455); var array3={}; for(var i=0; i<array1.length; i++){ if( (index= array2.indexOf(array1[i])) > 0 ){ if(!array3[array1[i]]) array3[array1[i]]= [] array3[array1[i]].push({index1:i,index2:index}); } } document.write(JSON.stringify(array3, undefined, 2)); console.log(array3);
Maybe try looping through the array and every time you find a match sending out the alert 也许尝试遍历数组,每次找到匹配项发出警报
alert(searchValue + " found with index(" + $.inArray(searchValue, array2) + ") in array");
(Sorry I couldn't put this as a comment, I don't have 50 reputation yet. (对不起,我无法对此发表评论,我还没有50名声望。
Use the regular javascript way to get an index from an Array instead of these angular things: 使用常规的javascript方法从数组获取索引,而不是从以下角度获取内容:
array.indexOf( value )
This will return the index of the value, or -1
if it is not found. 这将返回值的索引,如果未找到则返回-1
。
function checkIfExist(value, array) {
var index = array.indexOf(value);
if (index === -1) {
alert(searchValue + " not found");
} else {
alert(value + " found with index(" + index + ") in array");
}
}
What about something like this? 那这样的东西呢? See the jsfiddle : 参见jsfiddle :
var arr = [3545, 4355, 5435, 3457, 4355, 4355],
arr2 = [1022, 4355, 2333, 5656, 1234, 5555, 7777, 3455],
occurences = {};
arr2.forEach(function(el, i, array) {
occurences[el] = $.map(arr, function(element, index) {
return element == el ? index : null;
});
});
console.log(occurences);
$('body').html('Indexes: '+JSON.stringify(occurences));
// ==> Indexes: {"1022":[],"1234":[],"2333":[],"3455":[],"4355":[1,4,5],"5555":[],"5656":[],"7777":[]}
Basically, this loops through arr2
to match each of those values to a value in arr
. 基本上,这会循环遍历arr2
以将每个值与arr
的值进行匹配。 Each value from arr2
is made a key to a new object, called occurences
, and there is an array of the indexes of arr
where the values match the ones that are in arr2
. 从每个值arr2
由一键一个新对象,称为occurences
,并且存在的索引的阵列arr
其中的值相匹配的是在的那些arr2
。
As you can see, the only value from arr2
that appears in arr
is 4355 , and it occurs at the indexes of 1, 4, and 5 . 如您所见, arr2
中出现在arr
的唯一值是4355 ,它出现在索引1、4和5上 。 Since none of the other values from arr2
match those in arr
, the arrays of indexes for those values are simply empty. 由于来自arr2
的其他值均与arr
值不匹配,因此这些值的索引数组只是空的。
Obviously you can pretty the code up a bit and adjust it as it fits your needs. 显然,您可以对代码进行一些修饰,并根据需要进行调整。
$.inArray
gives the index only for the first occurrence in array starting from 0
else -1
. $.inArray
仅给出从0
else -1
开始的数组中第一次出现的索引。
function checkIfExist(searchValue, searchArray){
var index = [];//trace all matching index
for(var i = 0, l = searchArray.length; l > i; i ++) {
if(searchValue === searchArray[i]) {//matched value
index.push(i);//add index to array
}
}
return index;
}
var arr = checkIfExist(0, [1,0,3,9,0]);
if(arr.length) {//match found
alert('Matched at index: ' + arr.join(','))
} else {
alert('No match found');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.