【问题标题】:Reorder an array from a specific data从特定数据重新排序数组
【发布时间】:2020-12-10 15:27:52
【问题描述】:

我完全不知道我可以写哪个标题。 实际上,这是我从 API 中得到的:

[
    {
      "order": 1,
      "role": {
        "label": "singer"
      },
      "artist": {
        "name": "AaRON"
      }
    },
    {
      "order": 1,
      "role": {
        "label": "author"
      },
      "artist": {
        "name": "Simon Buret"
      }
    },
    {
      "order": 2,
      "role": {
        "label": "author"
      },
      "artist": {
        "name": "Olivier Coursier"
      }
    },
    {
      "order": 1,
      "role": {
        "label": "composer"
      },
      "artist": {
        "name": "John Doe"
      }
    }
  ]

这是我需要发送的:

"artist": {
  "singer": [
    "AaRON"
  ],
  "author": [
     "Simon Buret",
     "Olivier Coursier"
  ]
}

当然要考虑order属性。

示例:Simon Buret 是第一项,因为他的订单设置为 1。

我完全不知道如何实现,我只是做了一张地图,但不知道里面放什么:/

this.artistControl.controls.map(artistControl => {
   ...
});

有什么方法可以做我需要的吗?

【问题讨论】:

  • 你最好的选择是做你的研究,search 以获取有关 SO 的相关主题,然后试一试。 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布您的尝试minimal reproducible example,并具体说明您卡在哪里。人们会很乐意提供帮助。你也可以解释为什么空白作曲家也带有"order": 1 被遗漏了(我猜是因为名字是空白的,但是......)。
  • 你有什么理由忽略作曲家?
  • 作曲家是空的,因为我正在使用的 API 很差......我会更新我的帖子并填写这个属性,因为它似乎打扰了你。

标签: javascript angular typescript sorting filter


【解决方案1】:

这对你有用吗:

let arr = [
    { "order": 1, "role": { "label": "singer" }, "artist": { "name": "AaRON" } },
    { "order": 1, "role": { "label": "author" }, "artist": { "name": "Simon Buret" } },
    { "order": 2, "role": { "label": "author" }, "artist": { "name": "Olivier Coursier" } },
    { "order": 1, "role": { "label": "composer" }, "artist": { "name": "John Doe" } }
];

let obj = {'artist': {}};
arr.forEach(a => {
    obj['artist'][a.role.label] = obj['artist'][a.role.label] || [];
    obj['artist'][a.role.label][a.order-1] = a.artist.name;
});

console.log(obj);

【讨论】:

  • 是的,我认为它可以工作!我的一些值处于“禁用”模式,因为它们是表单值,但我可以调整你所做的我认为!谢谢!
【解决方案2】:

您可以使用reduce 方法将对象作为累加器参数,然后检查键是否不存在,使用空数组作为值创建它,然后按顺序添加名称。

const data = [{"order":1,"role":{"label":"singer"},"artist":{"name":"AaRON"}},{"order":1,"role":{"label":"author"},"artist":{"name":"Simon Buret"}},{"order":2,"role":{"label":"author"},"artist":{"name":"Olivier Coursier"}},{"order":1,"role":{"label":"composer"},"artist":{"name":"John Doe"}}]

const result = data.reduce((r, {
  role: { label },
  artist: { name },
  order
}) => {
  if (name) {
    if (!r[label]) r[label] = [];
    r[label][order - 1] = name;
  }
  
  return r;
}, {})

console.log(result)

【讨论】:

  • 您的解决方案对我来说更复杂。但是我实现了它并且它也在工作。谢谢 ! :)
【解决方案3】:

const array = [{"order":1,"role":{"label":"singer"},"artist":{"name":"AaRON"}},{"order":1,"role":{"label":"author"},"artist":{"name":"Simon Buret"}},{"order":2,"role":{"label":"author"},"artist":{"name":"Olivier Coursier"}},{"order":1,"role":{"label":"composer"},"artist":{"name":"John Doe"}}];

const result = array
  .sort((item1, item2) => item1.order - item2.order)
  .reduce((acc, { role, artist }) => ({
    ...acc,
    artist: {
      ...acc.artist,
      [role.label]: [
        ...(acc.artist[role.label] || []),
        artist.name,
      ],
    },
  }), { artist: {} });
  
console.log(result);

【讨论】:

    【解决方案4】:

    这是 es5 的另一种方法

    const data = [{ "order": 1, "role": { "label": "singer" }, "artist": { "name": "AaRON" } }, { "order": 1, "role": { "label": "author" }, "artist": { "name": "Simon Buret" } }, { "order": 2, "role": { "label": "author" }, "artist": { "name": "Olivier Coursier" } }, { "order": 1, "role": { "label": "composer" }, "artist": { "name": "John Doe" } }];
    
     var result = data.reduce(function(map, obj) {
                map["artist"] = map["artist"] || {};
                if (obj.role.label === 'author' || obj.role.label === 'singer') {
                    map["artist"][obj.role.label] = map["artist"][obj.role.label] || [];
                    map["artist"][obj.role.label][obj.order - 1] = obj.artist.name;
                }
                return map;
     }, {});
            
     console.log(result)

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 2017-12-27
      • 2022-01-02
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      相关资源
      最近更新 更多