Clean way of close and reopen tag when variable change in React
up vote
1
down vote
favorite
I have an array like this:
[
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
and I want to group the array based on its group. The array is already sorted by group and name. So, I make something like this:
let lastgroup = c[0].group
return <Group title={lastgroup}>
{c.map(x => {
if (lastgroup == x.group) {
return <a href={"#" + x.id}>{x.name}</a>
} else {
lastgroup = x.group
return </Group><Group title={lastgroup}><a href={"#" + x.id}>{x.name}</a> //-> What should I put here for something like this?
}
})}
</Group>
Now, that solution above does not work obviously, and is not clean. How should I close and reopen Tag? Or is there a cleaner way to do that?
Thank you
javascript arrays typescript2.0
add a comment |
up vote
1
down vote
favorite
I have an array like this:
[
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
and I want to group the array based on its group. The array is already sorted by group and name. So, I make something like this:
let lastgroup = c[0].group
return <Group title={lastgroup}>
{c.map(x => {
if (lastgroup == x.group) {
return <a href={"#" + x.id}>{x.name}</a>
} else {
lastgroup = x.group
return </Group><Group title={lastgroup}><a href={"#" + x.id}>{x.name}</a> //-> What should I put here for something like this?
}
})}
</Group>
Now, that solution above does not work obviously, and is not clean. How should I close and reopen Tag? Or is there a cleaner way to do that?
Thank you
javascript arrays typescript2.0
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an array like this:
[
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
and I want to group the array based on its group. The array is already sorted by group and name. So, I make something like this:
let lastgroup = c[0].group
return <Group title={lastgroup}>
{c.map(x => {
if (lastgroup == x.group) {
return <a href={"#" + x.id}>{x.name}</a>
} else {
lastgroup = x.group
return </Group><Group title={lastgroup}><a href={"#" + x.id}>{x.name}</a> //-> What should I put here for something like this?
}
})}
</Group>
Now, that solution above does not work obviously, and is not clean. How should I close and reopen Tag? Or is there a cleaner way to do that?
Thank you
javascript arrays typescript2.0
I have an array like this:
[
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
and I want to group the array based on its group. The array is already sorted by group and name. So, I make something like this:
let lastgroup = c[0].group
return <Group title={lastgroup}>
{c.map(x => {
if (lastgroup == x.group) {
return <a href={"#" + x.id}>{x.name}</a>
} else {
lastgroup = x.group
return </Group><Group title={lastgroup}><a href={"#" + x.id}>{x.name}</a> //-> What should I put here for something like this?
}
})}
</Group>
Now, that solution above does not work obviously, and is not clean. How should I close and reopen Tag? Or is there a cleaner way to do that?
Thank you
javascript arrays typescript2.0
javascript arrays typescript2.0
asked Nov 12 at 0:39
Magician
5473926
5473926
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
To clean the implementation up, I would suggest an intermediate process that groups sub-arrays via Array#reduce()
, followed by a mapping over the Object#entries()
of the reduced result where you would "wrap" items with the <Group />
tags.
The reduce step groups the data into an object, by the group
key. Each grouping contains an array of items for that group (these items are JSX snippets "created" during the grouping stage).
The second step is mapping over entries of that "reduced object". This allows you to wrap <Group/>
tags around the group items collected for each group during the prior reduce step, without the need for the lastgroup
idea:
// Component state / data
var data = [
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
// Component render method
render() {
return Object.entries(data
.reduce((grouping, item) => {
// Insert items for group if not present
if( !grouping[ item.group ] ) {
grouping[ item.group ] = ;
}
var groupItems = grouping[ item.group ];
// Create each group item, as we build up the items for this group
groupItems.push(<a href={"#" + item.id}>{item.name}</a>);
return grouping;
}, {}))
.map((entry) => {
// entry[0] is the group key, entry[0] is the group value (item array)
const group = entry[0];
const items = entry[1];
// Wrap items of each group with <Group/> tags
return <Group title={group}>{ items }</Group>
})
}
Thank you... This should work.
– Magician
Nov 12 at 17:35
add a comment |
up vote
0
down vote
Step 1, group entries by attribute group
.
See How to group an array of objects by key for more information
const groups = data.reduce((reducer, current) => {
reducer[current.group] = reducer[current.group] ||
reducer[current.group].push(current)
return reducer
}, {})
/*
console.log(groups)
should give you
{ A:
[ { group: 'A', id: '1', name: 'Mike' },
{ group: 'A', id: '6', name: 'Sherley' }
],
B:
[ { group: 'B', id: '3', name: 'Charlie' } ],
C:
[ { group: 'C', id: '2', name: 'Dave' } ]
}
*/
Step 2, render the groups
const keys = Object.keys(groups)
return (<div>
{keys.map(key => {
const currentGroup = groups[key]
// now render entries inside each group
return (<Group key={key} title={key}>
{currentGroup.map(entry => {
return <a key={entry.id} href={`#${entry.id}`}>{entry.name}</a>
})}
</Group>)
})}
</div>)
Actually, both method are the same. I accept the other because he answered first. Thank you
– Magician
Nov 12 at 17:35
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
To clean the implementation up, I would suggest an intermediate process that groups sub-arrays via Array#reduce()
, followed by a mapping over the Object#entries()
of the reduced result where you would "wrap" items with the <Group />
tags.
The reduce step groups the data into an object, by the group
key. Each grouping contains an array of items for that group (these items are JSX snippets "created" during the grouping stage).
The second step is mapping over entries of that "reduced object". This allows you to wrap <Group/>
tags around the group items collected for each group during the prior reduce step, without the need for the lastgroup
idea:
// Component state / data
var data = [
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
// Component render method
render() {
return Object.entries(data
.reduce((grouping, item) => {
// Insert items for group if not present
if( !grouping[ item.group ] ) {
grouping[ item.group ] = ;
}
var groupItems = grouping[ item.group ];
// Create each group item, as we build up the items for this group
groupItems.push(<a href={"#" + item.id}>{item.name}</a>);
return grouping;
}, {}))
.map((entry) => {
// entry[0] is the group key, entry[0] is the group value (item array)
const group = entry[0];
const items = entry[1];
// Wrap items of each group with <Group/> tags
return <Group title={group}>{ items }</Group>
})
}
Thank you... This should work.
– Magician
Nov 12 at 17:35
add a comment |
up vote
0
down vote
accepted
To clean the implementation up, I would suggest an intermediate process that groups sub-arrays via Array#reduce()
, followed by a mapping over the Object#entries()
of the reduced result where you would "wrap" items with the <Group />
tags.
The reduce step groups the data into an object, by the group
key. Each grouping contains an array of items for that group (these items are JSX snippets "created" during the grouping stage).
The second step is mapping over entries of that "reduced object". This allows you to wrap <Group/>
tags around the group items collected for each group during the prior reduce step, without the need for the lastgroup
idea:
// Component state / data
var data = [
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
// Component render method
render() {
return Object.entries(data
.reduce((grouping, item) => {
// Insert items for group if not present
if( !grouping[ item.group ] ) {
grouping[ item.group ] = ;
}
var groupItems = grouping[ item.group ];
// Create each group item, as we build up the items for this group
groupItems.push(<a href={"#" + item.id}>{item.name}</a>);
return grouping;
}, {}))
.map((entry) => {
// entry[0] is the group key, entry[0] is the group value (item array)
const group = entry[0];
const items = entry[1];
// Wrap items of each group with <Group/> tags
return <Group title={group}>{ items }</Group>
})
}
Thank you... This should work.
– Magician
Nov 12 at 17:35
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
To clean the implementation up, I would suggest an intermediate process that groups sub-arrays via Array#reduce()
, followed by a mapping over the Object#entries()
of the reduced result where you would "wrap" items with the <Group />
tags.
The reduce step groups the data into an object, by the group
key. Each grouping contains an array of items for that group (these items are JSX snippets "created" during the grouping stage).
The second step is mapping over entries of that "reduced object". This allows you to wrap <Group/>
tags around the group items collected for each group during the prior reduce step, without the need for the lastgroup
idea:
// Component state / data
var data = [
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
// Component render method
render() {
return Object.entries(data
.reduce((grouping, item) => {
// Insert items for group if not present
if( !grouping[ item.group ] ) {
grouping[ item.group ] = ;
}
var groupItems = grouping[ item.group ];
// Create each group item, as we build up the items for this group
groupItems.push(<a href={"#" + item.id}>{item.name}</a>);
return grouping;
}, {}))
.map((entry) => {
// entry[0] is the group key, entry[0] is the group value (item array)
const group = entry[0];
const items = entry[1];
// Wrap items of each group with <Group/> tags
return <Group title={group}>{ items }</Group>
})
}
To clean the implementation up, I would suggest an intermediate process that groups sub-arrays via Array#reduce()
, followed by a mapping over the Object#entries()
of the reduced result where you would "wrap" items with the <Group />
tags.
The reduce step groups the data into an object, by the group
key. Each grouping contains an array of items for that group (these items are JSX snippets "created" during the grouping stage).
The second step is mapping over entries of that "reduced object". This allows you to wrap <Group/>
tags around the group items collected for each group during the prior reduce step, without the need for the lastgroup
idea:
// Component state / data
var data = [
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
// Component render method
render() {
return Object.entries(data
.reduce((grouping, item) => {
// Insert items for group if not present
if( !grouping[ item.group ] ) {
grouping[ item.group ] = ;
}
var groupItems = grouping[ item.group ];
// Create each group item, as we build up the items for this group
groupItems.push(<a href={"#" + item.id}>{item.name}</a>);
return grouping;
}, {}))
.map((entry) => {
// entry[0] is the group key, entry[0] is the group value (item array)
const group = entry[0];
const items = entry[1];
// Wrap items of each group with <Group/> tags
return <Group title={group}>{ items }</Group>
})
}
// Component state / data
var data = [
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
// Component render method
render() {
return Object.entries(data
.reduce((grouping, item) => {
// Insert items for group if not present
if( !grouping[ item.group ] ) {
grouping[ item.group ] = ;
}
var groupItems = grouping[ item.group ];
// Create each group item, as we build up the items for this group
groupItems.push(<a href={"#" + item.id}>{item.name}</a>);
return grouping;
}, {}))
.map((entry) => {
// entry[0] is the group key, entry[0] is the group value (item array)
const group = entry[0];
const items = entry[1];
// Wrap items of each group with <Group/> tags
return <Group title={group}>{ items }</Group>
})
}
// Component state / data
var data = [
{
group: "A",
id: "1",
name: "Mike"
},
{
group: "A",
id: "6",
name: "Sherley"
},
{
group: "B",
id: "3",
name: "Charlie"
},
{
group: "C",
id: "2",
name: "Dave"
}
]
// Component render method
render() {
return Object.entries(data
.reduce((grouping, item) => {
// Insert items for group if not present
if( !grouping[ item.group ] ) {
grouping[ item.group ] = ;
}
var groupItems = grouping[ item.group ];
// Create each group item, as we build up the items for this group
groupItems.push(<a href={"#" + item.id}>{item.name}</a>);
return grouping;
}, {}))
.map((entry) => {
// entry[0] is the group key, entry[0] is the group value (item array)
const group = entry[0];
const items = entry[1];
// Wrap items of each group with <Group/> tags
return <Group title={group}>{ items }</Group>
})
}
edited Nov 12 at 1:17
answered Nov 12 at 0:51
Dacre Denny
9,3084729
9,3084729
Thank you... This should work.
– Magician
Nov 12 at 17:35
add a comment |
Thank you... This should work.
– Magician
Nov 12 at 17:35
Thank you... This should work.
– Magician
Nov 12 at 17:35
Thank you... This should work.
– Magician
Nov 12 at 17:35
add a comment |
up vote
0
down vote
Step 1, group entries by attribute group
.
See How to group an array of objects by key for more information
const groups = data.reduce((reducer, current) => {
reducer[current.group] = reducer[current.group] ||
reducer[current.group].push(current)
return reducer
}, {})
/*
console.log(groups)
should give you
{ A:
[ { group: 'A', id: '1', name: 'Mike' },
{ group: 'A', id: '6', name: 'Sherley' }
],
B:
[ { group: 'B', id: '3', name: 'Charlie' } ],
C:
[ { group: 'C', id: '2', name: 'Dave' } ]
}
*/
Step 2, render the groups
const keys = Object.keys(groups)
return (<div>
{keys.map(key => {
const currentGroup = groups[key]
// now render entries inside each group
return (<Group key={key} title={key}>
{currentGroup.map(entry => {
return <a key={entry.id} href={`#${entry.id}`}>{entry.name}</a>
})}
</Group>)
})}
</div>)
Actually, both method are the same. I accept the other because he answered first. Thank you
– Magician
Nov 12 at 17:35
add a comment |
up vote
0
down vote
Step 1, group entries by attribute group
.
See How to group an array of objects by key for more information
const groups = data.reduce((reducer, current) => {
reducer[current.group] = reducer[current.group] ||
reducer[current.group].push(current)
return reducer
}, {})
/*
console.log(groups)
should give you
{ A:
[ { group: 'A', id: '1', name: 'Mike' },
{ group: 'A', id: '6', name: 'Sherley' }
],
B:
[ { group: 'B', id: '3', name: 'Charlie' } ],
C:
[ { group: 'C', id: '2', name: 'Dave' } ]
}
*/
Step 2, render the groups
const keys = Object.keys(groups)
return (<div>
{keys.map(key => {
const currentGroup = groups[key]
// now render entries inside each group
return (<Group key={key} title={key}>
{currentGroup.map(entry => {
return <a key={entry.id} href={`#${entry.id}`}>{entry.name}</a>
})}
</Group>)
})}
</div>)
Actually, both method are the same. I accept the other because he answered first. Thank you
– Magician
Nov 12 at 17:35
add a comment |
up vote
0
down vote
up vote
0
down vote
Step 1, group entries by attribute group
.
See How to group an array of objects by key for more information
const groups = data.reduce((reducer, current) => {
reducer[current.group] = reducer[current.group] ||
reducer[current.group].push(current)
return reducer
}, {})
/*
console.log(groups)
should give you
{ A:
[ { group: 'A', id: '1', name: 'Mike' },
{ group: 'A', id: '6', name: 'Sherley' }
],
B:
[ { group: 'B', id: '3', name: 'Charlie' } ],
C:
[ { group: 'C', id: '2', name: 'Dave' } ]
}
*/
Step 2, render the groups
const keys = Object.keys(groups)
return (<div>
{keys.map(key => {
const currentGroup = groups[key]
// now render entries inside each group
return (<Group key={key} title={key}>
{currentGroup.map(entry => {
return <a key={entry.id} href={`#${entry.id}`}>{entry.name}</a>
})}
</Group>)
})}
</div>)
Step 1, group entries by attribute group
.
See How to group an array of objects by key for more information
const groups = data.reduce((reducer, current) => {
reducer[current.group] = reducer[current.group] ||
reducer[current.group].push(current)
return reducer
}, {})
/*
console.log(groups)
should give you
{ A:
[ { group: 'A', id: '1', name: 'Mike' },
{ group: 'A', id: '6', name: 'Sherley' }
],
B:
[ { group: 'B', id: '3', name: 'Charlie' } ],
C:
[ { group: 'C', id: '2', name: 'Dave' } ]
}
*/
Step 2, render the groups
const keys = Object.keys(groups)
return (<div>
{keys.map(key => {
const currentGroup = groups[key]
// now render entries inside each group
return (<Group key={key} title={key}>
{currentGroup.map(entry => {
return <a key={entry.id} href={`#${entry.id}`}>{entry.name}</a>
})}
</Group>)
})}
</div>)
answered Nov 12 at 1:18
Rico Chen
67348
67348
Actually, both method are the same. I accept the other because he answered first. Thank you
– Magician
Nov 12 at 17:35
add a comment |
Actually, both method are the same. I accept the other because he answered first. Thank you
– Magician
Nov 12 at 17:35
Actually, both method are the same. I accept the other because he answered first. Thank you
– Magician
Nov 12 at 17:35
Actually, both method are the same. I accept the other because he answered first. Thank you
– Magician
Nov 12 at 17:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254677%2fclean-way-of-close-and-reopen-tag-when-variable-change-in-react%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown