Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何按属性聚合嵌套JavaScript对象?_Javascript - Fatal编程技术网

如何按属性聚合嵌套JavaScript对象?

如何按属性聚合嵌套JavaScript对象?,javascript,Javascript,我有以下JavaScript对象: var example = [{ country: "US", things: { weather: 'cloudy' } }, { country: "US", things: { resource: 'lead', weather: 'sunny' } }, { country: "MX", things: { weather:

我有以下JavaScript对象:

var example = [{
    country: "US",
    things: {
      weather: 'cloudy'
    }
  },
  {
    country: "US",
    things: {
      resource: 'lead',
      weather: 'sunny'
    }
  },
  {
    country: "MX",
    things: {
      weather: 'sunny'
    }
  },
  {
    country: "MX",
    things: {
      resource: 'gold',
      weather: 'sunny'
    }
  },
  {
    country: "MX",
    things: {
      resource: 'copper'
    }
  },
]
我想通过聚合转换成这种格式

var out = [{
    country_code: 'US',
    things: {
      resource: ['lead'],
      weather: ['cloudy', 'sunny']
    }
  },
  {
    country_code: 'MX',
    things: {
      resource: ['gold', 'copper'],
      weather: ['sunny'],
    }
  }

]

我曾经尝试过使用reduce和map的组合,但没有效果。如果此示例还可以作为数据操作的一般策略的起点(可能涉及也可能不涉及数组方法的使用),那就太好了。

使用
reduce
在对象上迭代以编译新对象,将您想要的东西拼凑在一起:

const示例=[{
国家:“美国”,
事情:{
天气:“多云”
}
},
{
国家:“美国”,
事情:{
资源:“潜在客户”,
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
资源:“黄金”,
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
资源:“铜”
}
},
]
const out=Object.values(
例如:减少((a,v)=>{
如果(!一个[v.国家]){
国家{
国家代码:v.country,
事物:{}
}
}
Object.entries(v.things).forEach(([key,value])=>{
如果(!a[v.country].事物[key]){
a[v.country]。事物[key]=[]
}
如果(!a[v.country].things[key].includes(value)){
a[v.country]。事物[key]。推送(值)
}
})
归还
}, {})
)

console.log(out)
这将向您展示如何使用reduce来获取使用reduce所需的数据

const示例=[{
国家:“美国”,
事情:{
天气:“多云”
}
},
{
国家:“美国”,
事情:{
资源:“领导”,
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
资源:“黄金”,
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
资源:“铜”
}
}
];
常量输出=示例。减少((acc,电流)=>{
const index=acc.findIndex(x=>x.country==current.country);
如果(索引==-1){
常数newNode={
国家:当前国家,
事情:{
资源:current.things.resource?[current.things.resource]:[],
天气:时事天气?[时事天气]:[]
}
};
acc.push(新节点);
}否则{
current.things.resource&&acc[index].things.resource.findIndex(x=>x==current.things.resource)==-1&&acc[index].things.resource.push(current.things.resource)
current.things.weather&&acc[index].things.weather.findIndex(x=>x==current.things.weather)==-1&&acc[index].things.weather.push(current.things.weather)
}
返回acc;
}, []);

控制台日志(输出)您可以使用
reduce
功能和
findIndex
检查累加器是否有带有
国家/地区代码的对象。如果存在,则更新
对象中的数组

var示例=[{
国家:“美国”,
事情:{
天气:“多云”
}
},
{
国家:“美国”,
事情:{
资源:“潜在客户”,
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
资源:“黄金”,
天气:“晴朗”
}
},
{
国家:“MX”,
事情:{
资源:“铜”
}
},
]
最终功能(arr){
返回arr.reduce(功能(acc,curr){
设findIndex=acc.findIndex(功能(项目){
返回项目所在国\代码===当前所在国;
});
如果(findIndex==-1){
加速推({
国家/地区代码:当前国家/地区,
事情:{
资源:curr.things.resource?[curr.things.resource]:[],
天气:时事天气?[时事天气]:[]
}
})
}否则{
if(curr.things.resource&&acc[findIndex].things.resource.indexOf(curr.things.resource)=-1){
acc[findIndex].things.resource.push(curr.things.resource);
}
if(curr.things.weather&&acc[findIndex].things.weather.indexOf(curr.things.weather)=-1){
acc[findIndex].things.weather.push(curr.things.weather);
}
}
返回acc;
}, [])
}

console.log(finalOut(示例))
使用reduce迭代所有项目,然后以正确的格式排列它们:

example.reduce((prev,current)=>{

        let index = prev.findIndex(item => item.country_code == current.country);

        if(index>=0){
            if(current.things.resource && !prev[index].things.resource.includes(current.things.resource))
                prev[index].things.resource.push(current.things.resource);
            if(current.things.weather && !prev[index].things.weather.includes(current.things.weather))
                prev[index].things.weather.push(current.things.weather);
        }else{
            prev.push({
                country_code : current.country,
                things : {
                    weather : current.things.weather ? [current.things.weather] : [],
                    resource : current.things.resource ? [current.things.resource] : []
                }
            });
        }
        return prev;
},[]);
  • 使用数据库构建国家数据地图
  • {
    美国:{
    资源:[“潜在客户],
    天气:[“多云”、“晴朗”],
    },
    MX:{
    资源:[“黄金”、“铜”],
    天气:['晴朗'],
    },
    }
    
  • 用于从映射中获取条目数组
  • [
    ['US',{资源:['lead'],天气:['Cloud','sunny']},
    ['MX',{资源:['gold',copper'],天气:['sunny']},
    ]
    
  • 将条目数组映射到具有所需结构的对象数组中
  • constbuildcountriesmap=data=>data.reduce((地图,{country,things:{weather,resource}})=>{
    如果(!map.hasOwnProperty(国家)){
    地图[国家]={资源:[],天气:[]};
    }
    const{resource:mapResource,weather:mapWeather}=map[国家];
    if(资源&!mapResource.includes(资源)){
    mapResource.push(资源);
    }
    if(天气和地图天气,包括(天气)){
    mapWeather.push(天气);
    }
    返回图;
    }, {});
    const merge=data=>Object.entries(buildCountriesMap(数据))
    .map(([country,things])=>({country,things}));
    常量示例=[
    {
    国家:"美国",,
    事情:{
    天气:“多云”,
    },
    },
    {
    国家:"美国",,
    事情:{
    资源:“潜在客户”,
    天气:“晴朗”,
    },
    },
    {
    国家:'MX',
    事情:{
    天气:“晴朗”,
    },
    },
    {
    国家:'MX',
    事情:{
    资源:“黄金”,
    天气:“晴朗”,
    },
    },
    {
    国家:'MX',
    事情:{
    资源:“铜”,
    },
    },
    ];
    
    log(合并(示例))到目前为止您尝试了什么?你能把代码贴在这里吗