0

I am struggling with a problem. I want to Spilt javascript array to another javascript arrays using javascript. The array should be split the last element of the array. it means ['/Local1', 'Local1', 'Local'] and ['/Local2', 'Local2','Local'] should contain in Local array. ['/Forign1', 'Forign1', 'Forign'] and ['/Forign1', 'Forign1', 'Forign'] 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'],
        ['/Forign1', 'Forign1', 'Forign'],
        ['/Forign2', 'Forign2','Forign'],
      ]
  ]

  const xxx = getMenuGroup(selected, menu);
}

function getMenuGroup(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;
 },{});
 console.log(result);
 //return result;
}

Current Output:

[
  [
    { href: '/Local1', label: 'Local1', active: ' active' },
    { href: '/Local2', label: 'Local2', active: '' },
    { href: '/Forign1', label: 'Forign1', active: '' },
    { href: '/Forign2', label: 'Forign2', active: '' }
  ]
]

Expected output:

{
  Local: [
    { href: '/Local1', label: 'Local1', active: ' active' },
    { href: '/Local2', label: 'Local2', active: '' }
  ],
  Forign: [
    { href: '/Forign1', label: 'Forign1', active: '' },
    { href: '/Forign2', label: 'Forign2', active: '' }
  ],
}

2 Answers 2

4

This is a fairly straightforward reduce operation.

const input = 
    [
      ['/Local1', 'Local1', 'Local'],
      ['/Local2', 'Local2','Local'],
      ['/Forign1', 'Forign1', 'Forign'],
      ['/Forign2', 'Forign2','Forign'],
    ];
    
 var selected = '/Local1'; 
 

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;
},{});

console.log(result);

Sign up to request clarification or add additional context in comments.

3 Comments

I added your code to getMenuGroup function. But I am getting { '[object Object]': [ { href: [Object], label: [Object], active: '' } ] } as output. What is my wrong there. I updated my quection with your answer.
I think In your input one [ ] is not there.
@user_v12 so make that input[0].reduce(...)
0

You're just missing a single line in the processMenuGroup function.

function processMenuGroup(item, selected) {
             return {
                 'href': item[0],
                 'label': item[1],
                 'group': item[2],
                 'active': (selected == item[0]) ? ' active' : ''
             };
}

In particular, the line that sets the "group" property for each of the objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.