在javascript中对具有偏移的对象数组进行排序

时间:2018-04-30 13:31:59

标签: javascript jquery arrays angularjs

我是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" 
},

那么,我该怎么做?任何人都可以帮我这个吗?

3 个答案:

答案 0 :(得分:1)

首先在单独的对象中使用startOffsetendOffset创建所需的数组,然后您可以为aValbVal分配值,以便在{ {1}}函数如下面的代码所示:

sort

答案 1 :(得分:1)

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



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




答案 2 :(得分:0)

首先在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)
  )
)