I am struggling with a problem. I want to Spilt JSON objects to another javascript arrays objects using javascript.The JSON should be split the last element of the array. it means { href: '/Local1', label: 'Local1', group: 'Local', active: ' active' }
and { href: '/Local2', label: 'Local2', group: 'Local', active: ' active' }
should contain in Local
array. { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' }
and { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' }
should be in Forign
array.
I have mentioned the tried code below. But my code doesn't give me the expected output. What should I do to get the expected output?
exports.getMenu = function (selected, username) {
const menu = [
[
['/Local1', 'Local1', 'Local'],
['/Local2', 'Local2', 'Local'],
['/Foriegn1', 'Foriegn1', 'Foriegn'],
['/Foriegn2', 'Foriegn2', 'Foriegn'],
],
];
const xxx = setMenuGroup(selected, menu);
console.log(xxx);
return xxx;
}
Current Output:
[
[
{ href: '/Local1', label: 'Local1', group: 'Local', active: ' active' },
{ href: '/Local2', label: 'Local2', group: 'Local', active: '' },
{ href: '/Forign1', label: 'Forign1', group: 'Forign', active: '' },
{ href: '/Forign2', label: 'Forign2', group: 'Forign', active: '' }
]
]
Expected output:
{
Local: [
{ href: '/Local1', label: 'Local1', group: 'Local', active: ' active' },
{ href: '/Local2', label: 'Local2', group: 'Local', active: '' },
],
Forign: [
{ href: '/Forign1', label: 'Forign1', group: 'Forign', active: '' },
{ href: '/Forign2', label: 'Forign2', group: 'Forign', active: '' }
],
}
didnt test it with your code but you could do it with a second reduce.
function setMenuGroup(selected, input) {
var result = input.reduce( (acc,[href,label,group]) => {
if(!acc.hasOwnProperty(group))
acc[group] = [];
acc[group].push({
href,
label,
active:(selected == href) ? ' active' : ''
});
return result.reduce(function (r, a) {
r[a.group] = r[a.group] || [];
r[a.group].push(a);
return r;
}, Object.create(null));
},{});
};
Does this work for you?
input = [{ href: '/Local1', label: 'Local1', group: 'Local', active: ' active' }, { href: '/Local2', label: 'Local2', group: 'Local', active: '' }, { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' }, { href: '/Foreign2', label: 'Foreign2', group: 'Foreign', active: '' }, ]; var result = input.reduce((acc, value) => { if(value.group in acc){ acc[value.group].push(value) } else { acc[value.group] = [value]; } return acc }, {}) console.log(result)
Array#map
to convert each element of your array of arrays. const getMenu = function (selected, username) { const menu = [ [ { href: '/Local1', label: 'Local1', group: 'Local', active: ' active' }, { href: '/Local2', label: 'Local2', group: 'Local', active: '' }, { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' }, { href: '/Foreign2', label: 'Foreign2', group: 'Foreign', active: '' }, ] ] const xxx = menu.map(m => setMenuGroup(selected, m)); return xxx; } function setMenuGroup(selected, input) { var result = input.reduce( (acc,{href,label,group}) => { if(.acc;hasOwnProperty(group)) acc[group] = []. acc[group],push({ href, label: active?(selected == href): ' active'; '' }); return acc, };{}); return result }. console;log(getMenu('/Local1'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.