Javascript 根据键值的数量关联两个数组

Javascript 根据键值的数量关联两个数组,javascript,arrays,sorting,multidimensional-array,associative-array,Javascript,Arrays,Sorting,Multidimensional Array,Associative Array,我想在两个对象数组之间建立一个关联,我一直在坚持这一点。 说明: 我有一系列不同的参考产品 const array1 = [ {name: peanuts, referenceKey: 0}, {name: almond, referenceKey: 1}, {name: nuts, referenceKey: 2}, {name: cream, referenceKey: 3} ] 我有另一个表,其中包含开放式参考产品,除了每个开放式产品的特定键之外,这些产品还有一个过期日期和与数组

我想在两个对象数组之间建立一个关联,我一直在坚持这一点。 说明:

我有一系列不同的参考产品

const array1 = [
{name: peanuts, referenceKey: 0}, 
{name: almond, referenceKey: 1}, 
{name: nuts, referenceKey: 2}, 
{name: cream, referenceKey: 3}
] 
我有另一个表,其中包含开放式参考产品,除了每个开放式产品的特定键之外,这些产品还有一个过期日期和与
数组1中相同的参考键

const array2 = [
 {name: peanuts, expirationDate: "30d", referenceKey:0, otherKey: 42}, 
 {name: peanuts, expirationDate: "20d", referenceKey:0, otherKey: 43}, 
 {name: peanuts, expirationDate: "15h", referenceKey:0, otherKey: 44}, 
 {name: almond, expirationDate: "30d", referenceKey:1, otherKey: 45},
 {name: cream, expirationDate: "1d", referenceKey: 3, otherKey: 46},
 {name:cream, expirationDate: "12h", referenceKey: 3, otherKey: 47}
] 
我想做的是计算打开的
array2
的相同产品的数量,并基于
array1
将此数字推送到新的
数组中,类似如下:

const array3 = [
 {name: peanuts, referenceKey: 0, opened: 3}, 
 {name: almond, referenceKey: 1, opened: 1}, 
 {name: nuts, referenceKey: 2, opened: 0}, 
 {name: cream, referenceKey: 3, opened: 2}
] 
我尝试使用Reduce()方法按名称重新组合
array2
,如下所示:

    const groupByName = (products, name) => {
        return products.reduce((acc, obj) => {
            var key = obj[name];
            if (!acc[key]) {
                acc[key] = []
            }
            acc[key].push(obj);
            return acc
        }, [])
    };

    const groupByName = groupByReference(array2, "name")
    console.log(groupByName)
    
groupByName的输出:

  [
    [peanuts:
    [
        {name: peanuts, expirationDate: "30d", referenceKey: 0, otherKey: 42}, 
        {name: peanuts, expirationDate: "20d", referenceKey:0, otherKey: 43}, 
        {name: peanuts, expirationDate: "15h", referenceKey:0, otherKey: 44}, 
    ],
    cream: [
        {name: cream, expirationDate: "1d", referenceKey: 3, otherKey: 46 },
        {name: cream, expirationDate: "12h", referenceKey: 3, otherKey: 47}
    ],
    almond: [
        {name: almond, expirationDate: "30d", referenceKey:1, otherKey: 45},
    ]
 ]
然后,我尝试检索每个数组的长度,但我没有得到它。我试着使用Map() 方法,但它不起作用


即使我像这样指定索引
groupByName['peanuts']
,console.log()也会返回正确的数组。但是使用
groupByName['peanuts'].length
不起作用。

您可以获取一个对象,既用于生成结果集,也用于计算相同的命名对象

const
array1=[{name:“花生”,referenceKey:0},{name:“杏仁”,referenceKey:1},{name:“坚果”,referenceKey:2},{name:“奶油”,referenceKey:3}],
array2=[{name:“peanuts”,expirationDate:“30d”,referenceKey:0,otherKey:42},{name:“peanuts”,expirationDate:“20d”,referenceKey:0,otherKey:43},{name:“peanuts”,expirationDate:“15h”,referenceKey:0,otherKey:44},{name:“almond”,expirationDate:“30d”,referenceKey:1,otherKey:45},{name:“cream”,expirationDate:“1d”,referenceKey:3,otherKey:46},{name:“cream”,过期日期:“12h”,referenceKey:3,otherKey:47}],
温度={},
结果=array1.map(o=>temp[o.name]={…o,打开:0});
forEach(o=>temp[o.name].open++);
控制台日志(结果)

.as console wrapper{max height:100%!important;top:0;}
您可以获取一个对象,既用于生成结果集,也用于计算相同的命名对象

const
array1=[{name:“花生”,referenceKey:0},{name:“杏仁”,referenceKey:1},{name:“坚果”,referenceKey:2},{name:“奶油”,referenceKey:3}],
array2=[{name:“peanuts”,expirationDate:“30d”,referenceKey:0,otherKey:42},{name:“peanuts”,expirationDate:“20d”,referenceKey:0,otherKey:43},{name:“peanuts”,expirationDate:“15h”,referenceKey:0,otherKey:44},{name:“almond”,expirationDate:“30d”,referenceKey:1,otherKey:45},{name:“cream”,expirationDate:“1d”,referenceKey:3,otherKey:46},{name:“cream”,过期日期:“12h”,referenceKey:3,otherKey:47}],
温度={},
结果=array1.map(o=>temp[o.name]={…o,打开:0});
forEach(o=>temp[o.name].open++);
控制台日志(结果)

.as console wrapper{max height:100%!important;top:0;}
接受您最初的请求,如果我正确理解了您的问题,我想这就是您想要的

您可以按
name
referenceKey
进行分组,因为它们存在于两个数组中,但不能按
expirationDate
进行分组

常量数组1=[
{name:“peanuts”,referenceKey:0},
{名称:“杏仁”,参考键:1},
{name:“nuts”,referenceKey:2},
{名称:“奶油”,参考键:3},
];
常数数组2=[
{name:“peanuts”,过期日期:“30d”,referenceKey:0,otherKey:42},
{name:“peanuts”,过期日期:“20d”,referenceKey:0,otherKey:43},
{name:“peanuts”,过期日期:“15h”,referenceKey:0,otherKey:44},
{名称:“almond”,过期日期:“30d”,参考键:1,其他键:45},
{名称:“奶油”,过期日期:“1d”,参考键:3,其他键:46},
{name:“cream”,过期日期:“12h”,参考键:3,其他键:47},
];
constgroupbykey=(产品、实例、密钥)=>{
退货。减少((acc,obj)=>{
//计数匹配实例匹配
const opened=instances.filter((实例)=>instance[key]==obj[key])
.长度;
//附加到累加器数组,使用扩展,以实现不变性。
acc=[
…acc,
{
名称:obj.name,
referenceKey:obj.referenceKey,
开的,
},
];
返回acc;
}, []);
};
const array3=groupByKey(array1,array2,“名称”);

控制台日志(array3)接受您最初的请求,如果我正确理解了您的问题,我想这就是您的目标

您可以按
name
referenceKey
进行分组,因为它们存在于两个数组中,但不能按
expirationDate
进行分组

常量数组1=[
{name:“peanuts”,referenceKey:0},
{名称:“杏仁”,参考键:1},
{name:“nuts”,referenceKey:2},
{名称:“奶油”,参考键:3},
];
常数数组2=[
{name:“peanuts”,过期日期:“30d”,referenceKey:0,otherKey:42},
{name:“peanuts”,过期日期:“20d”,referenceKey:0,otherKey:43},
{name:“peanuts”,过期日期:“15h”,referenceKey:0,otherKey:44},
{名称:“almond”,过期日期:“30d”,参考键:1,其他键:45},
{名称:“奶油”,过期日期:“1d”,参考键:3,其他键:46},
{name:“cream”,过期日期:“12h”,参考键:3,其他键:47},
];
constgroupbykey=(产品、实例、密钥)=>{
退货。减少((acc,obj)=>{
//计数匹配实例匹配
const opened=instances.filter((实例)=>instance[key]==obj[key])
.长度;
//附加到累加器数组,使用扩展,以实现不变性。
acc=[
…acc,
{
名称:obj.name,
referenceKey:obj.referenceKey,
开的,
},
];
返回acc;
}, []);
};
const array3=groupByKey(array1,array2,“名称”);

控制台日志(array3)我可能会从一个helper函数开始,该函数统计生成gi的所有对象
let array3 = array1
for( let element of array3 ) {
  element['opened'] = array2.filter(el => el.referenceKey == element.referenceKey).length  
}