从另一个对象创建对象数组
Creating an arrayof Object from another object
我有一个包含电子邮件对象及其属性的对象。
我对 mime 属性 () 很感兴趣,我可以使用 array[index].subtype 访问它。
我想创建一个新的 (MIME) 对象数组,其中只包含 mime 和计数。
var mimeType = []
var arr = {"object with emails"}.
arr.forEach(function(elem, index, array){
if(mimeType.indexOf(array[index].subtype == -1)){
mimeType.push({mime: array[index].subtype, `count:1});
}
else{
mimeType[count] = count+1;
}
它打印所有的 mime,重复
这是你想要的吗? :
var tmp = {};
var arr = [{ subtype: 'pdf' }, { subtype: 'doc' }, { subtype: 'pdf' }, { subtype: 'pdf' }];
arr.forEach(function(elem, index, array){
if(tmp[elem.subtype] === undefined){
tmp[elem.subtype] = 1;
}
else {
tmp[elem.subtype]++;
}
});
var mimeType = Object.getOwnPropertyNames(tmp).map(function(e) { return { mime: e, count: tmp[e] }});
mimeType 的结果 =
[{mime:"pdf",count:3},{mime:"doc",count:1}]
我有一个包含电子邮件对象及其属性的对象。 我对 mime 属性 () 很感兴趣,我可以使用 array[index].subtype 访问它。 我想创建一个新的 (MIME) 对象数组,其中只包含 mime 和计数。
var mimeType = []
var arr = {"object with emails"}.
arr.forEach(function(elem, index, array){
if(mimeType.indexOf(array[index].subtype == -1)){
mimeType.push({mime: array[index].subtype, `count:1});
}
else{
mimeType[count] = count+1;
}
它打印所有的 mime,重复
这是你想要的吗? :
var tmp = {};
var arr = [{ subtype: 'pdf' }, { subtype: 'doc' }, { subtype: 'pdf' }, { subtype: 'pdf' }];
arr.forEach(function(elem, index, array){
if(tmp[elem.subtype] === undefined){
tmp[elem.subtype] = 1;
}
else {
tmp[elem.subtype]++;
}
});
var mimeType = Object.getOwnPropertyNames(tmp).map(function(e) { return { mime: e, count: tmp[e] }});
mimeType 的结果 =
[{mime:"pdf",count:3},{mime:"doc",count:1}]