Dev*_*ate 4 javascript arrays jquery unique object
假设我们有以下内容:
node[1].name = "apple";
node[1].color = "red";
node[2].name = "cherry";
node[2].color = "red";
node[3].name = "apple";
node[3].color = "green";
node[4].name = "orange";
node[4].color = "orange;
Run Code Online (Sandbox Code Playgroud)
如果我使用jQuery.unique(节点),我将获得所有原始节点,因为它们都有不同的名称或颜色.我想要做的只是获取具有唯一名称的节点,该名称应返回
node[1] (apple)
node[2] (cherry)
node[4] (orange)
Run Code Online (Sandbox Code Playgroud)
它不应该返回3,因为它是相同的水果,即使我们有绿色和红色的苹果.
使用Array.filter和临时数组来存储dups:
function filterByName(arr) {
var f = []
return arr.filter(function(n) {
return f.indexOf(n.name) == -1 && f.push(n.name)
})
}
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/mbest/D6aLV/6/
| 归档时间: |
|
| 查看次数: |
4340 次 |
| 最近记录: |