【问题标题】:sorting outer array of object by key value in the internal array object按内部数组对象中的键值对对象的外部数组进行排序
【发布时间】:2019-10-04 23:29:24
【问题描述】:

我有一个对象数组,它在另一个 obj 数组中,如何根据内部数组中的相等 actionId 键对该数组进行排序?

这是我原来的数组:

const arrayOfItems = {
    items: [
        {
            item: '1',
            anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
        },
        {
            item: '2',
            anotherArray: []
        },
        {
            item: '3',
            anotherArray: []
        },
        {
            item: '4',
            anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
        },
        {
            item: '5',
            anotherArray: []
        },
        {
            item: '6',
            anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
        }
    ]
};

结果应该是所有具有相同 actionId 在彼此下的项目

sortedArray = {
    items: [
    {
       item: '1',
       anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
    },
    {
      item: '4',
      anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
    },
    {
      item: '6',
      anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
    },
    ...
]
};

这是我尝试过的:

const sortingArray = arrayOfItems.items.sort((a, b) => {
  return a.anotherArray > 0 && a.anotherArray[0].actionId.localeCompare(b.anotherArray[0].actionId);
})

【问题讨论】:

  • 发布的问题似乎根本不包括任何解决问题的尝试。 Stack Overflow 期待您 try to solve your own problem first,因为您的尝试有助于我们更好地了解您想要什么。请编辑问题以显示您尝试过的内容,以说明您在minimal reproducible example 中遇到的特定障碍。欲了解更多信息,请参阅How to Ask 并拨打tour
  • 网络上有无数的教程正是针对这种情况,只需快速搜索即可帮助您完成作业。干杯。
  • @ChrisW。我找了但是没找到,能不能给我写个网址
  • a.discounts > 0 应该是a.discounts.length > 0
  • 我认为你不能用 sort() 做到这一点。比较函数只能指定元素的相对顺序,不能强制组合在一起。

标签: javascript


【解决方案1】:

这样的事情可以解决问题。这基于 ActionId 排序,然后是 item。没有 actionId 的项目将被移动到数组的末尾。

const arrayOfItems = {items: [{item: '1', anotherArray: [{actionId: '1234-dh4t-tr21-6sw8'}]}, {item: '2', anotherArray: []}, {item: '3', anotherArray: []}, {item: '4', anotherArray: [{actionId: '1234-dh4t-tr21-6sw8'}]}, {item: '5', anotherArray: []}, {item: '6', anotherArray: [{actionId: '1234-dh4t-tr21-6sw8'}]}]};

arrayOfItems.items.sort((a, b) => {
  const lenA = a.anotherArray.length,
        lenB = b.anotherArray.length;

  // order based on item if both a and b don't have an actionId
  if (!lenA && !lenB) return a.item - b.item;
  // move the element without actionId towards the end if a or b doesn't have an actionId
  if (!lenA || !lenB) return lenB - lenA;

  const actionIdA = a.anotherArray[0].actionId,
        actionIdB = b.anotherArray[0].actionId;

  // order based on item if both a and b have the same actionId
  if (actionIdA === actionIdA) return a.item - b.item;

  // order based on actionId
  return actionIdA.localeCompare(actionIdB);
});

console.log(arrayOfItems.items);

如果你不关心按item秒排序,可以去掉:

// order based on item if both a and b don't have an actionId
if (!lenA && !lenB) return a.item - b.item;

还有:

// order based on item if both a and b have the same actionId
if (actionIdA === actionIdA) return a.item - b.item;

【讨论】:

  • 如果我们有不同的actionId,它将无法正确排序
  • @YAllaban 将具有相同 actionId 的元素放在一起。因此,如果您有操作 ID ["a", "b", "b", "a", "a"],它们将被排序为 ["a", "a", "a", "b", "b"]。后面是没有actionId的元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 2020-04-21
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多