有这样一个数组:
arr= [{"name":"Vasya","class_id":"1","class":{"id":"1","class":"1"}},
{"name":"John","class_id":"1","class":{"id":"1","class":"1"}},
{"name":"Smith","class_id":"2","class":{"id":"2","class":"2"}}];
您能否告诉我们如何引入一系列独特的 class 值?
答案 0 :(得分:0)
您想要返回类对象的某些属性的唯一值,还是仅返回唯一的类对象?
以下代码将执行后者,但只有在所有属性都具有唯一值(或者对象具有不同属性)时才返回类对象:
var arrOfUniqueClassObjects = arr.reduce(function (newArr, obj) {
var isInArr = newArr.some(function (inArr) {
return Object.keys(inArr).some(function (key) {
return obj.class.hasOwnProperty(key) && obj.class[key] === inArr[key];
});
});
if (!isInArr) newArr.push(obj.class);
return newArr;
}, []);