Check if an item exists in the array
本问题已经有最佳答案,请猛点这里访问。
我需要检查主数组中是否存在特定的值。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 | var hideFilters = function() { var listCategoryId = ['1000014', '1000015', '1000016', '1000017', '1000018', '1000019', '1000021', '1000086']; var actualCategoryId = '1000018'; if (actualCategoryId === listCategoryId) { console.log('is equal'); } else { console.log('fuen... fuen...'); } }; hideFilters(); |
1 2 3 | if(listCategoryId.indexOf(actualCategoryId) != -1) { console.log('exists') } |
如果
1 2 3 4 5 6 7 8 9 10 11 12 13 | var hideFilters = function() { var listCategoryId = ['1000014', '1000015', '1000016', '1000017', '1000018', '1000019', '1000021', '1000086']; var actualCategoryId = '1000018'; if (listCategoryId.indexOf(actualCategoryId) > -1) { console.log('is equal'); } else { console.log('fuen... fuen...'); } }; hideFilters(); |
你可以用$.inarray
1 2 3 4 5 6 7 | var listCategoryId = ['1000014', '1000015', '1000016', '1000017', '1000018', '1000019', '1000021', '1000086']; var actualCategoryId = '1000018'; if ($.inArray(actualCategoryId, listCategoryId) != -1) { console.log('exists'); } |
1 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> |