DP
Size: a a a
DP
DP
Д
t
DP
DP
DT
const simplaArr = [
{year: '1988', title: 'tile1', location: 'b'},
{year: '1988', title: 'title2', location: 'a'},
{year: '1989', title: 'title3', location: 'c'}
];
=>
const simpleArr2 = [
{
year: '1988',
places: [
{title: 'title1', location: 'b'},
{title: 'title2', location: 'a'},
]
},
{
year: '1989',
places: [{title: 'title3', location: 'c'}]
}]
S
const simplaArr = [
{year: '1988', title: 'tile1', location: 'b'},
{year: '1988', title: 'title2', location: 'a'},
{year: '1989', title: 'title3', location: 'c'}
];
=>
const simpleArr2 = [
{
year: '1988',
places: [
{title: 'title1', location: 'b'},
{title: 'title2', location: 'a'},
]
},
{
year: '1989',
places: [{title: 'title3', location: 'c'}]
}]
DP
DP
EP
const simplaArr = [
{year: '1988', title: 'tile1', location: 'b'},
{year: '1988', title: 'title2', location: 'a'},
{year: '1989', title: 'title3', location: 'c'}
];
=>
const simpleArr2 = [
{
year: '1988',
places: [
{title: 'title1', location: 'b'},
{title: 'title2', location: 'a'},
]
},
{
year: '1989',
places: [{title: 'title3', location: 'c'}]
}]
const simpleArr = [
{ year: "1988", title: "tile1", location: "b" },
{ year: "1988", title: "title2", location: "a" },
{ year: "1989", title: "title3", location: "c" }
];
const newObj = {};
simpleArr.forEach((el) => {
const year = el.year;
const t = el.title;
const loc = el.location;
if (!Array.isArray(newObj[year])) newObj[year] = [];
newObj[year].push({ title: t, location: loc });
});
const result =[]
for(let year in newObj){
result.push({ year: year, places: newObj[year] })
}
console.log(result)
S
const simpleArr = [
{ year: "1988", title: "tile1", location: "b" },
{ year: "1988", title: "title2", location: "a" },
{ year: "1989", title: "title3", location: "c" }
];
const newObj = {};
simpleArr.forEach((el) => {
const year = el.year;
const t = el.title;
const loc = el.location;
if (!Array.isArray(newObj[year])) newObj[year] = [];
newObj[year].push({ title: t, location: loc });
});
const result =[]
for(let year in newObj){
result.push({ year: year, places: newObj[year] })
}
console.log(result)
В
const simplaArr = [
{year: '1988', title: 'tile1', location: 'b'},
{year: '1988', title: 'title2', location: 'a'},
{year: '1989', title: 'title3', location: 'c'}
];
=>
const simpleArr2 = [
{
year: '1988',
places: [
{title: 'title1', location: 'b'},
{title: 'title2', location: 'a'},
]
},
{
year: '1989',
places: [{title: 'title3', location: 'c'}]
}]
Object.values(simplaArr.reduce((map, { year, ...place }) =>
Object.assign(map, {
[year]: {
year, places: (
map[year] &&
map[year].places || []
).concat(place)
}
}), {}
))
S
Object.values(simplaArr.reduce((map, { year, ...place }) =>
Object.assign(map, {
[year]: {
year, places: (
map[year] &&
map[year].places || []
).concat(place)
}
}), {}
))
EP
Object.values(simplaArr.reduce((map, { year, ...place }) =>
Object.assign(map, {
[year]: {
year, places: (
map[year] &&
map[year].places || []
).concat(place)
}
}), {}
))
SM