如何在javascripts或jquery中获取数组上的唯一数据?

如何在javascripts或jquery中获取数组上的唯一数据?,javascript,jquery,Javascript,Jquery,我有这样的数组: var arr = [ {"id":1,"tipe":"foo"}, {"id":2,"tipe":"bar"}, {"id":3,"tipe":"bar"} ]; [{"id":1,"tipe":"foo"},{"id":2,"tipe":"bar"}]; 如何在javascript或jquery上获取唯一数据,并按如下方式显示结果: var arr = [ {"id":1,"tipe":"foo"}, {"id":2,"tipe":"bar"}, {"id":3,"ti

我有这样的数组:

var arr = [
{"id":1,"tipe":"foo"},
{"id":2,"tipe":"bar"},
{"id":3,"tipe":"bar"}
];
[{"id":1,"tipe":"foo"},{"id":2,"tipe":"bar"}];
如何在javascript或jquery上获取唯一数据,并按如下方式显示结果:

var arr = [
{"id":1,"tipe":"foo"},
{"id":2,"tipe":"bar"},
{"id":3,"tipe":"bar"}
];
[{"id":1,"tipe":"foo"},{"id":2,"tipe":"bar"}];
这是我获取数据的代码:

$('#tipe').change(function(){
                    var val = $(this).val();
                    url = '{{url('getdata')}}';
                    $.ajax({
                        url:url,
                        data: {tipe:val}
                    }).done(function(data){
                        $('#page').empty();
                        $.each(data, function(key, value){
                            $('#page').append(value['halaman']);
                        });
                    })
                });

您可以尝试使用此代码

var result = arr.reduce(function(t, el1) {
  var matches = t.filter(function(el2) {
    return el1.tipe == el2.tipe
  })
  if (matches.length == 0)
    t.push(el1)
  return t;
}, [])
演示

var-arr=[{
“id”:1,
“tipe”:“foo”
},
{
“id”:2,
“tipe”:“bar”
},
{
“id”:3,
“tipe”:“bar”
}
];
var结果=arr.reduce(函数(t,el1){
var matches=t.filter(函数(el2){
返回el1.tipe==el2.tipe
})
if(matches.length==0)
t、 推动(el1)
返回t;
}, [])

console.log(结果)
您可以尝试使用此代码

var result = arr.reduce(function(t, el1) {
  var matches = t.filter(function(el2) {
    return el1.tipe == el2.tipe
  })
  if (matches.length == 0)
    t.push(el1)
  return t;
}, [])
演示

var-arr=[{
“id”:1,
“tipe”:“foo”
},
{
“id”:2,
“tipe”:“bar”
},
{
“id”:3,
“tipe”:“bar”
}
];
var结果=arr.reduce(函数(t,el1){
var matches=t.filter(函数(el2){
返回el1.tipe==el2.tipe
})
if(matches.length==0)
t、 推动(el1)
返回t;
}, [])

console.log(result)
unique基于什么标准?可能这有助于您输入以下链接:unique基于什么标准?可能这有助于您输入以下链接:可能值得在其中添加一些注释来解释,但其他方面都很好answer@MasterYoda是的,我会在一秒钟内发表一些评论也许值得在那里发表一些评论来解释,但在其他方面很好answer@MasterYoda是的,我会马上发表一些评论