在javascript中从数组中删除重复的数组值
Remove duplicate array value from array in javascript
我想从我的数组中删除重复的条目,而我的数组是
ArrayTotal All Banks,Total All Banks,Total Domestic Banks,Total Domestic Banks,B2B Bank,B2B Bank,Bank of Montreal,Bank of Montreal,The Bank of Nova Scotia,The Bank of Nova Scotia,
我们想删除重复的条目,我正在php unique_array中尝试,我也在javascript 中尝试
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
console.log(uniqueNames) its gave error Unexpected token A
尝试php、
$dup=array();
foreach($bname as $k=>$v) {
if( ($kt=array_search($v,$bname))!==false and $k!=$kt )
{ unset($unique[$kt]); $dup[]=$v; }
}
这样尝试此处演示
var names = ["Total All Banks","Total All Banks","Total Domestic Banks","Total Domestic Banks","B2B Bank","B2B Bank","Bank of Montreal","Bank of Montreal","The Bank of Nova Scotia","The Bank of Nova Scotia"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
console.log(uniqueNames);
您应该能够使用array_unique在PHP中实现这一点,如下所示:
// Your existing array
$items = [ "Total All Banks", "Total All Banks", "Total Domestic Banks", "Total Domestic Banks", "B2B Bank", "B2B Bank", "Bank of Montreal", "Bank of Montreal", "The Bank of Nova Scotia", "The Bank of Nova Scotia" ];
// array_unique does the dirty work for you
$noduplicates = array_unique($items);
// results are in $noduplicates
print_r($noduplicates);
这里是在PHP中,没有array_unique:
// Your existing array
$items = [ "Total All Banks", "Total All Banks", "Total Domestic Banks", "Total Domestic Banks", "B2B Bank", "B2B Bank", "Bank of Montreal", "Bank of Montreal", "The Bank of Nova Scotia", "The Bank of Nova Scotia" ];
// Our new array for items
$noduplicates = [];
// Loop through all items in an array
foreach($items as $item) {
// Check new array to see if it's there
if(!in_array($item, $noduplicates)) {
// It's not, so add it
$noduplicates[] = $item;
}
}
// results are in $noduplicates
print_r($noduplicates);
这里是用Javascript编写的-您不需要使用jQuery来完成此任务:
// Your existing array
var items = [ "Total All Banks", "Total All Banks", "Total Domestic Banks", "Total Domestic Banks", "B2B Bank", "B2B Bank", "Bank of Montreal", "Bank of Montreal", "The Bank of Nova Scotia", "The Bank of Nova Scotia" ];
// Our new array for items
var noduplicates = [];
// Loop through all items in an array
for (var i = 0; i < items.length; i++) {
// Check new array to see if it's already there
if(noduplicates.indexOf(items[i]) == -1) {
// add to the new array
noduplicates.push(items[i]);
}
}
// results are in noduplicates
console.log(noduplicates);
Fiddle在这里可用。
相关文章:
- 从组件状态的数组中删除元素
- Mongoose-在更新中删除数组元素
- 从数组中删除元素的最佳方法是:javascript/jquery
- 添加和删除隐藏字段数组中的值,而不提交表单
- 从数组中删除重复条目,并在javascript中按顺序排列
- 一些元素没有从数组中删除
- 删除JS数组中的最小数字
- 从数组中删除三元组项
- 删除重复的数组值,然后存储它们[反应]
- 在特定索引处剥离/删除数组中的值
- 数组删除重复结果Javascript
- 字符串替换或通过数组删除
- js中的对象数组:删除重复项
- KnockoutJS.Mapping.FromJS-可观察数组-删除不起作用
- 用于垃圾收集的Javascript数组删除
- 为什么”——数组.删除数组的最后一个元素
- PHP循环通过脚本数组&删除项目
- 谷歌地图API:试图使用数组删除多个标记
- 带有for循环+条件的Javascript数组删除不必要的元素
- 在JavaScript中管理像ArrayList这样的数组:删除一个元素并关闭创建的间隙