簡體   English   中英

將 JSON 對象溢出到 Javascript 中的 JSON 數組對象

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

我正在努力解決一個問題。 我想使用 ZDE9B9ED78D7E2E19DCEEFFEE780E2F9 將 JSON 對象溢出到另一個 javascript arrays 對象 JSON 應該拆分數組的最后一個元素。 它表示{ 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: '' }應該在Forign數組中。

我在下面提到了嘗試過的代碼。 但是我的代碼沒有給我預期的 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;

}

當前 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: '' }
  ]
]

預期 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: '' }
  ],
}

沒有用你的代碼測試它,但你可以用第二次減少來做。

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

 },{});
};

這對你有用嗎?

 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. 您應該使用Array#map來轉換 arrays 數組的每個元素。
  2. 減少時使用 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