gpt4 book ai didi

javascript - 如何使用 Javascript 获取元素为对象的唯一数组

转载 作者:行者123 更新时间:2023-11-29 21:52:16 24 4
gpt4 key购买 nike

下面是我的一个数组的例子

var mm =  [
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"IMG_0245.JPG","text":"IMG_0245.JPG","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"IMG_0246.JPG","text":"IMG_0246.JPG","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"IMG_0247.JPG","text":"IMG_0247.JPG","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"IMG_0248.JPG","text":"IMG_0248.JPG","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"IMG_0249.JPG","text":"IMG_0249.JPG","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"IMG_0250.JPG","text":"IMG_0250.JPG","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"IMG_0251.JPG","text":"IMG_0251.JPG","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"jingling.rar","text":"jingling.rar","parent":"Junk"},
{"id":"Junk","text":"Junk","parent":"#"},
{"id":"pics.rar","text":"pics.rar","parent":"Junk"}
];

如果您注意到,数组中的第一个和第三个对象是相同的,依此类推。我查找了内置函数,但找不到。在 jQuery 或 Javascript 中是否有任何简单的内置简单函数来执行此操作?

最佳答案

你可以这样做:

Array.prototype.getUnique = function(){
var o = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(o.hasOwnProperty(JSON.stringify(this[i]))) {
continue;
}
a.push(this[i]);
o[JSON.stringify(this[i])] = 1;
}
return a;
}

这将返回一个仅包含唯一值的数组。我不认为它有任何内置功能。<​​/p>

关于javascript - 如何使用 Javascript 获取元素为对象的唯一数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28526229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com