Javascript 对嵌套对象数组中的唯一元素进行计数
我需要找到一种快速有效的方法,按生产日期统计此阵列中不同SKU的数量以及重复的总次数。请:-Javascript 对嵌套对象数组中的唯一元素进行计数,javascript,arrays,Javascript,Arrays,我需要找到一种快速有效的方法,按生产日期统计此阵列中不同SKU的数量以及重复的总次数。请:- var testArray = [ {fields: {"production date": "2020-09-22", "sku": "22-09-2020 cocobear | sand | yrs 1-3"}}, {fields: {"production date": "2020-09-
var testArray = [
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 cocobear | sand | yrs 1-3"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 misha | mustard | mth 6-12"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 0-3"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 0-3"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 0-3"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 0-3"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 0-3"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 3-6"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 3-6"}},
{fields: {"production date": "2020-09-22", "sku": "22-09-2020 taylor | moon | mth 3-6"}},
{fields: {"production date": "2020-10-06", "sku": "06-10-2020 gloves | grey-marle"}},
{fields: {"production date": "2020-10-06", "sku": "06-10-2020 gloves | grey-marle"}},
{fields: {"production date": "2020-10-06", "sku": "06-10-2020 madison | grey"}},
{fields: {"production date": "2020-10-06", "sku": "06-10-2020 madison | grey"}},
{fields: {"production date": "2020-10-06", "sku": "06-10-2020 madison | grey"}},
{fields: {"production date": "2020-10-06", "sku": "06-10-2020 madison | grey"}}
];
output.inspect(testArray);
预期的输出将是另一个如下所示的数组:-
resultArray = [
{"production date": "2020-09-22",
"skus": 4,
"items": 10},
{"production date": "2020-10-06",
"skus": 2,
"items": 6}
];
output.inspect(resultArray);
由于testArray中的嵌套对象数组,我一直在挣扎
有什么想法吗
谢谢
Jonathan不确定这是否是解决此问题的最佳代码段,顺便说一句,它解决了问题:)
我还假设,如果两个“SKUT”的内容不同,它们的位置也不同。这里有一个可能有效的建议
const testArray=[
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020 cocobear | sand | yrs 1-3”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020米沙|芥末| mth 6-12”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 0-3”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 0-3”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 0-3”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 0-3”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 0-3”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 3-6”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 3-6”},
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020泰勒|月亮| mth 3-6”},
{字段:{“生产日期”:“2020-10-06”,“sku”:“06-10-2020手套|灰色大理石”},
{字段:{“生产日期”:“2020-10-06”,“sku”:“06-10-2020手套|灰色大理石”},
{字段:{“生产日期”:“2020-10-06”,“sku”:“06-10-2020麦迪逊|格雷”},
{字段:{“生产日期”:“2020-10-06”,“sku”:“06-10-2020麦迪逊|格雷”},
{字段:{“生产日期”:“2020-10-06”,“sku”:“06-10-2020麦迪逊|格雷”},
{字段:{“生产日期”:“2020-10-06”,“sku”:“06-10-2020麦迪逊|灰色”}
];
让结果=testArray.reduce((c,项)=>{
const date=项目字段[“生产日期];
常量sku=项目字段['sku'];
如果(!c.hasOwnProperty(日期)){
c[日期]={
“生产日期”:日期,
项目:0,
库存单位:0,
集合:新集合()
};
}
c[日期].items++;
c[日期].set.add(sku);
c[date].skus=c[date].set.size;
返回c;
}, {});
结果=对象值(结果);
控制台日志(结果)代码>我的方式
const结果=
reduce((a,{fields},i,t)=>
{
如果(!a.some(x=>x['production date']===字段['production date']))
{
让prods=t.filter(x=>x.fields['production date']==fields['production date']))
.map(m=>m.fields.sku)
,skus=prods.filter((v,i,t)=>i==t.lastIndexOf(v))
;
a、 推送({'production date':字段['production date'],SKU:skus.length,items:prods.length})
}
归还
},[])
完整代码:
const testArray=
[{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 cocobear | sand | yrs 1-3'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020米沙|芥末| mth 6-12'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 0-3'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 0-3'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 0-3'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 0-3'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 0-3'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 3-6'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 3-6'}
,{字段:{‘生产日期’:'2020-09-22',sku:'22-09-2020 taylor | moon | mth 3-6'}
,{字段:{‘生产日期’:'2020-10-06',sku:'06-10-2020手套|灰色大理石'}
,{字段:{‘生产日期’:'2020-10-06',sku:'06-10-2020手套|灰色大理石'}
,{字段:{‘生产日期’:'2020-10-06',sku:'06-10-2020麦迪逊|灰色'}
,{字段:{‘生产日期’:'2020-10-06',sku:'06-10-2020麦迪逊|灰色'}
,{字段:{‘生产日期’:'2020-10-06',sku:'06-10-2020麦迪逊|灰色'}
,{字段:{‘生产日期’:'2020-10-06',sku:'06-10-2020麦迪逊|灰色'}
]
常数结果=
reduce((a,{fields},i,t)=>
{
如果(!a.some(x=>x['production date']===字段['production date']))
{
让prods=t.filter(x=>x.fields['production date']==fields['production date']))
.map(m=>m.fields.sku)
,skus=prods.filter((v,i,t)=>i==t.lastIndexOf(v))
;
a、 推送({'production date':字段['production date'],SKU:skus.length,items:prods.length})
}
归还
},[])
console.log(结果)
.as控制台包装{max height:100%!important;top:0;}
前面的大多数答案在我看来似乎有点太难了。
我想我们可以使用Set类和一个简单的reduce函数
const testArray=[
{字段:{“生产日期”:“2020-09-22”,“sku”:“22-09-2020 cocobear | sand | yrs”
const inspect = input => {
const sortedData = testArray
.map(item => item.fields)
};
const inspect = input => {
const sortedData = testArray
.map(item => item.fields)
.reduce((a, c) => {
const productionDate = c["production date"];
return {
...a,
[productionDate]: (a[productionDate] || []).concat(c.sku)
}
}, {})
};
// Final result
const inspect = input => {
const sortedData = testArray
.map(item => item.fields)
.reduce((a, c) => {
const productionDate = c["production date"];
return {
...a,
[productionDate]: (a[productionDate] || []).concat(c.sku)
}
}, {})
return Object.entries(sortedData).map(([k, v]) => ({
"production date": k,
items: v.length,
skus: new Set(v).size
}))
};
inspect(testArray);