简体   繁体   English

将 JSON 对象溢出到 Javascript 中的 JSON 数组对象

[英]Spilt JSON objects to JSON array objects in Javascript

I am struggling with a problem.我正在努力解决一个问题。 I want to Spilt JSON objects to another javascript arrays objects using javascript.我想使用 ZDE9B9ED78D7E2E19DCEEFFEE780E2F9 将 JSON 对象溢出到另一个 javascript arrays 对象The JSON should be split the last element of the array. JSON 应该拆分数组的最后一个元素。 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: '/Local1', label: 'Local1', group: 'Local', active: ' active' }{ href: '/Local2', label: 'Local2', group: 'Local', active: ' active' }应该包含在Local数组中。 { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' } and { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' } should be in Forign array. { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' } and { href: '/Foreign1', label: 'Foreign1', group: 'Foreign', active: '' }应该在Forign数组中。

I have mentioned the tried code below.我在下面提到了尝试过的代码。 But my code doesn't give me the expected output.但是我的代码没有给我预期的 output。 What should I do to get the expected output?我应该怎么做才能得到预期的 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:当前 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:预期 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)

  1. You should use Array#map to convert each element of your array of arrays.您应该使用Array#map来转换 arrays 数组的每个元素。
  2. Use object destructuring when reducing instead of array destructuring.减少时使用 object 解构而不是数组解构。

 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'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM