我有以下JavaScript对象:
var example = [{ 国家:“美国”, 事情:{ 天气:‘多云’ } }, { 国家:“美国”, 事情:{ 资源:‘领先’, 天气:…
使用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; },[]);
reduce
{ US: { resource: ['lead'], weather: ['cloudy', 'sunny'], }, MX: { resource: ['gold', 'copper'], weather: ['sunny'], }, }
Object.entries
[ [ 'US', { resource: ['lead'], weather: ['cloudy', 'sunny'] } ], [ 'MX', { resource: ['gold', 'copper'], weather: ['sunny'] } ], ]
const buildCountriesMap = data => data.reduce((map, { country, things: { weather, resource } }) => { if (!map.hasOwnProperty(country)) { map[country] = { resource: [], weather: [] }; } const { resource: mapResource, weather: mapWeather } = map[country]; if (resource && !mapResource.includes(resource)) { mapResource.push(resource); } if (weather && !mapWeather.includes(weather)) { mapWeather.push(weather); } return map; }, {}); const merge = data => Object.entries(buildCountriesMap(data)) .map(([country, things]) => ({ country, things })); const 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', }, }, ]; console.log(merge(example));
您可以使用 reduce 功能和用途 findIndex 检查累加器是否有对象 country_code 。如果它在那里然后更新数组 things 宾语。
findIndex
country_code
things
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' } }, ] function finalOut(arr) { return arr.reduce(function(acc, curr) { let findIndex = acc.findIndex(function(item) { return item.country_code === curr.country; }); if (findIndex === -1) { acc.push({ country_code: curr.country, things: { resource: curr.things.resource ? [curr.things.resource] : [], weather: curr.things.weather ? [curr.things.weather] : [] } }) } else { 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); } } return acc; }, []) } console.log(finalOut(example))
这将向您展示如何使用reduce来获取您想要使用reduce的数据
const 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" } } ]; const output = example.reduce((acc, current) => { const index = acc.findIndex(x => x.country === current.country); if (index === -1) { const newNode = { country: current.country, things: { resource: current.things.resource ? [current.things.resource] : [], weather: current.things.weather ? [current.things.weather] : [] } }; acc.push(newNode); } else { 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) } return acc; }, []); console.log(output);
使用 reduce 迭代对象以编译新对象,拼凑所需的东西:
const 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' } }, ] const out = Object.values( example.reduce((a, v) => { if (!a[v.country]) { a[v.country] = { country_code: v.country, things: {} } } Object.entries(v.things).forEach(([key, value]) => { if (!a[v.country].things[key]) { a[v.country].things[key] = [] } if (!a[v.country].things[key].includes(value)) { a[v.country].things[key].push(value) } }) return a }, {}) ) console.log(out)