JavaScript
- 10 ответов
- 0 вопросов
7
Вклад в тег
const data = [
{ name: 'Sasha', apple: 8, banana: 6, cherry: 9 },
{ name: 'Artur', apple: 4, banana: 2, cherry: 1 },
{ name: 'Sasha', apple: 5, banana: 7, cherry: 3 },
{ name: 'Bogdan', apple: 6, banana: 11, cherry: 8 },
{ name: 'Artur', apple: 8, banana: 6, cherry: 9 },
];
data.reduce((acc, item) => {
const { name, ...rest } = item;
if (!acc.hasOwnProperty(name)) acc[name] = { ...rest };
else {
const keys = Object.keys(rest);
keys.forEach(key => acc[name][key] += rest[key])
}
return acc
}, {})
const arr = [
{
contry: 'Russia',
name: 'Alex',
age: '34',
},
{
contry: 'Russia',
name: 'July',
age: '22',
},
{
contry: 'USA',
name: 'Tom',
age: '32',
},
{
contry: 'USA',
name: 'Jack',
age: '74',
},
]
arr.reduce((acc, item) => {
const { contry, name, age } = item;
if(!acc.hasOwnProperty(contry)) acc[contry] = {[name]:age}
else acc[contry] = {...acc[contry], [name]: age}
return acc
}, {})
const Button = ({ handler }) => (
<button onClick={handler}>Нажми на меня</button>
);
const App = () => {
const [element, setElement] = useState(1000000);
const handler = () => {
setElement(element - 1);
};
return (
<div className="page" id="page">
<div className="element">{element}</div>
<Button handler={handler} />
</div>
);
};
const data = [
{
id: 1,
name: "краски"
},
{
id: 1,
name: "краски"
},
{
id: 2,
name: "краски"
},
{
id: 1,
name: "краски"
},
]
Object.values(data.reduce((acc, item) => {
const { id } = item
if(!acc.hasOwnProperty(id)) acc[id] = { ...item, count: 1};
else acc[id].count += 1
return acc
}, {}))