我有一个元素列表,我希望有一个获取该列表的函数,处理它并返回具有相同名称的元素列表。我怎么能在Javascript或jquery中做到这一点?
[{ ID : 1, Name : "john" , BrandName : "Lg" },
{ ID : 2, Name : "john" , BrandName : "Lg" },
{ ID : 3, Name : "doe" , BrandName : "kenwood" },
{ ID : 4, Name : "peter" , BrandName : "Nokia" },
{ ID : 5, Name : "john" , BrandName : "Lg" }
{ ID : 6, Name : "ethan" , BrandName : "Sony" }
{ ID : 7, Name : "micheal" , BrandName : "Samsung" }];
答案 0 :(得分:0)
过滤您的表格以保留在表格中多次找到的元素:
var test = [{ ID : 1, Name : "john" , BrandName : "Lg" },
{ ID : 2, Name : "john" , BrandName : "Lg" },
{ ID : 3, Name : "doe" , BrandName : "kenwood" },
{ ID : 4, Name : "peter" , BrandName : "Nokia" },
{ ID : 5, Name : "john" , BrandName : "Lg" },
{ ID : 6, Name : "ethan" , BrandName : "Sony" },
{ ID : 7, Name : "micheal" , BrandName : "Samsung" }];
test.filter((el) => test.filter(_el => _el.Name === el.Name).length > 1)
// ->
// [
// {ID: 1, Name: "john", BrandName: "Lg"},
// {ID: 2, Name: "john", BrandName: "Lg"},
// {ID: 5, Name: "john", BrandName: "Lg"}
// ]