【问题标题】:Sort array of objects having the offsets in javascript在javascript中对具有偏移量的对象数组进行排序
【发布时间】:2018-10-10 14:45:31
【问题描述】:

我是web development 的新手。在这里,我确实有一个类似于

的数组
[{
        endOffset: "50"
        startOffset: "25"
        type: "Education"
    },
    {
        endOffset: "67"
        startOffset: "55"
        type: "address"
    },
    {
        endOffset: "65"
        startOffset: "59"
        type: "project"
    },
    {
        endOffset: "80"
        startOffset: "70"
        type:"fullname"
    }]

在这个对象数组中,现在我根据 endoffsets 对其进行排序。就像 ->

jsonDataArray.sort(function (a, b) {
            return a.startOffset - b.startOffset;
          });

然后它给了我排序的数组。但这里仅根据startoffset 进行排序。

我想要的是我想要一个像这样的数组,它将按 start 和 endoffset 的升序排序,这样数组就会像

[{
 starOffset: "25",
 type: "Education"
}, {
 endOffset: "50"
 type: "Education" },

{
 startoffset: "55"
 type: "address" },
{
 startoffset: "59"
 type: "project" },
{
 endoffset: "65"
 type: "project" },

{
 endoffset: "67"
 type: "address" 
},
{
 endoffset: "67"
 type: "address" 
},
{
 startOffset: "70"
 type: "address" 
},
{
 endoffset: "80"
 type: "address" 
},

]

那么,我该怎么做呢?有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你可以查看这个话题:“How to sort an array of objects by multiple fields?”,好像和你的问题有关。
  • 为了让您免于未来的头痛/问题:您的代码中不涉及 JSON - jsonDataArray 是一个对象数组,与 JSON 没有任何关系。
  • 好的,谢谢。为此。
  • 那么如果 startOffset 相同,你想按 endOffset 排序吗?
  • 所以,在这种情况下,首先出现的将首先添加到数组中

标签: javascript jquery arrays angularjs


【解决方案1】:

首先使用 startOffsetendOffset 在单独的对象中创建所需的数组,然后您可以通过在您的 sort 函数中设置条件来分配 aValbVal 的值,如下面的代码中所述:

var jsonDataArray = [{
        endOffset: "50",
        startOffset: "25",
        type: "Education"
    },
    {
        endOffset: "67",
        startOffset: "65",
        type: "address"
    },
    {
        endOffset: "65",
        startOffset: "59",
        type: "project"
    },
    {
        endOffset: "80",
        startOffset: "70",
        type:"fullname"
    }];

var newJSON = [];
jsonDataArray.forEach((obj)=>{
  var  tempObj = {
     endOffset: obj.endOffset,
     type: obj.type
  };
  newJSON.push(tempObj);
  tempObj = {
     startOffset: obj.startOffset,
     type: obj.type
  };
  newJSON.push(tempObj);
});
newJSON.sort(function (a, b) {
  var aVal = a.startOffset?a.startOffset:a.endOffset;
  var bVal = b.startOffset?b.startOffset:b.endOffset;
  return aVal - bVal;
});
console.log(newJSON);

【讨论】:

  • 这里第一个是我的 jsonData 数组。下一个是我预期的输出。
  • 嘿,如果偏移量相同会发生什么?
  • 它们仍然可以工作,因为如果发现对象中存在startOffset,它将使用该条件,否则它将使用endOffset,并且代码肯定会有@987654329 @或endOffset@ganeshkaspate
  • @ganeshkaspate 您可以使用它而不是真正抽象的代码,因为这将使您可以轻松更改代码,并且一目了然也更容易理解
  • 好的 Ankit。谢谢您的帮助。很好的解决方案。
【解决方案2】:

您可以创建一个包含开始值和结束值的新数组,并通过获取开始值或结束值对它们进行排序。

var data = [{ endOffset: "50", startOffset: "25", type: "Education" }, { endOffset: "67", startOffset: "55", type: "address" }, { endOffset: "65", startOffset: "59", type: "project" }, { endOffset: "80", startOffset: "70", type: "fullname" }],
    extended = data.reduce(
        (r, { endOffset, startOffset, type }) =>
            r.concat({ startOffset, type }, { endOffset, type }), []);

extended.sort((a, b) => (a.startOffset || a.endOffset) - (b.startOffset || b.endOffset));

console.log(extended);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES5

var data = [{ endOffset: "50", startOffset: "25", type: "Education" }, { endOffset: "67", startOffset: "55", type: "address" }, { endOffset: "65", startOffset: "59", type: "project" }, { endOffset: "80", startOffset: "70", type: "fullname" }],
    extended = data.reduce(function (r, o) {
        return r.concat(
            { startOffset: o.startOffset, type: o.type },
            { endOffset: o.endOffset, type: o.type }
        );
    }, []);

extended.sort(function (a, b) {
    return (a.startOffset || a.endOffset) - (b.startOffset || b.endOffset);
});

console.log(extended);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    【解决方案3】:

    以下排序先按 startOffset,然后按 endOffset:

    const array = [{
      endOffset: "50",
      startOffset: "25",
      type: "Education"
    },
    {
      endOffset: "67",
      startOffset: "55",
      type: "address"
    },
    {
      endOffset: "65",
      startOffset: "59",
      type: "project"
    },
    {
      endOffset: "80",
      startOffset: "70",
      type:"fullname"
    }]
    
    console.log(
      array.reduce(
        (result,item)=>result.concat([
          {startOffset:item.startOffset,type:item.type},
          {endOffset:item.endOffset,type:item.type}
        ]),
        []
      ).sort(
        (a,b)=>(a.startOffset||a.endOffset)-(b.startOffset||b.endOffset)
      )
    )

    【讨论】:

    • 我正在使用 ES5
    • @ganeshkaspate 然后你使用类型functrion(){return 而不是()=>
    • 这很好。实际上,我想以单独的方式拥有它,就像我给的数组一样。
    • 只要有升序,对我来说开始或结束都不会。
    • @ganeshkaspate 好的,我想我明白你想要什么。更新了答案。
    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2022-06-17
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多