如何通过两个不同的键对带有对象的数组进行排序?

时间:2019-05-16 11:17:38

标签: javascript arrays sorting object

我不知道如何比较两个键来对对象数组进行排序。我有数组:

const arr = [
  {
    age: "20",
    group: "XXX",
    id: "3L1aa1558002753379",
    menu: "standard",
    name: "Adam"
  },
  {
    age: "22",
    group: "XXX",
    id: "xhNt11558002753379",
    menu: "standard",
    name: "Ola"
  },
  {
    otherid: "3L1aa1558002753379",
    age: "25",
    group: "YYY",
    id: "6ryVK1558002753379",
    menu: "standard",
    name: "Wommman"
  },
  {
    otherid: "xhNt11558002753379",
    age: "25",
    group: "YYY",
    id: "aL1aa1558002753312",
    menu: "standard",
    name: "xxxxxy"
  },
  {
    age: "25",
    group: "YYY",
    id: "6ryVK1558002753379",
    menu: "standard",
    name: "xxxxxo"
  }
  ,
  {
    otherid: "1ryVK1558002753372",
    age: "25",
    group: "YYY",
    id: "9ryVK155a002753370",
    menu: "standard",
    name: "xxxxxo"
  },
  {
    age: "25",
    group: "YYY",
    id: "1ryVK1558002753372",
    menu: "standard",
    name: "xxxxxo"
  }
];

我想用这种方式进行排序:如果“ id”和“ otherid”相同,则让对象彼此相邻。我不知道该怎么做,有人会这么好吗?

就像这里:

    const arr = [
  {
    age: "20",
    group: "XXX",
    id: "3L1aa1558002753379",
    menu: "standard",
    name: "Adam"
  },
  {
    otherid: "3L1aa1558002753379",
    age: "25",
    group: "YYY",
    id: "6ryVK1558002753379",
    menu: "standard",
    name: "Wommman"
  },
  {
    age: "22",
    group: "XXX",
    id: "xhNt11558002753379",
    menu: "standard",
    name: "Ola"
  },
  {
    otherid: "xhNt11558002753379",
    age: "25",
    group: "YYY",
    id: "aL1aa1558002753312",
    menu: "standard",
    name: "xxxxxy"
  },
  {
    age: "25",
    group: "YYY",
    id: "1ryVK1558002753372",
    menu: "standard",
    name: "xxxxxo"
  },
  {
    otherid: "1ryVK1558002753372",
    age: "25",
    group: "YYY",
    id: "9ryVK155a002753370",
    menu: "standard",
    name: "xxxxxo"
  },
    {
    age: "25",
    group: "YYY",
    id: "6ryVK1558002753379",
    menu: "standard",
    name: "xxxxxo"
  }
  ,
];

我尝试了类似的操作:Javascript sort array by two fields,但失败了

1 个答案:

答案 0 :(得分:0)

您指出,您只需要计算对并将其呈现在react应用程序中。

以一种可以直接呈现数据的方式来结构化数据会更加有意义。

由于您可以控制数据,因此无需生成平面列表。您可以使用分层结构或嵌套对象来设置(学生或其他)对。

let students = [{name: 'Jon', id:0}, {name: 'Peter', id: 1}, {name: 'Steve', id:2}, {name: 'Joe', id: 3}]
let pairs = [{a: students [3], b: students[1]}, {a: students [2], b: students [0]}];
console.log (pairs);

现在,如果要渲染这些对,则已经在所需的结构中包含了数据。

render () {
      return pairs.map (pair => <Pair data={pair} />)
}

如果您愿意,还可以展平pairs数组,并在相邻元素之间绘制平坦的列表。

let students = [{name: 'Jon', id:0}, {name: 'Peter', id: 1}, {name: 'Steve', id:2}, {name: 'Joe', id: 3}]
let pairs = [{a: students [3], b: students[1]}, {a: students [0], b: students [2]}];
const flatten = (flat, {a, b}) => [...flat, a, b];
const sorted  = pairs.reduce (flatten, []);
console.log (sorted)

const Student = data => <div>{data.name}</div>
const Pair = pair => <div>
    <Student data={pair.a} />
    <Student data={pair.b} />
</div>
const renderFlat = () => {
     return sorted.map (student => <Student data={student} />
}
const renderPairs = () => {
     reutnr pairs.map (pair => <Pair data={pair} />)
}

我希望至少有一点道理。 -无论如何这里都是排序功能

function sort (arr)  {
   let otherids = arr.reduce ((lkp, obj) => {
       if (obj.otherid)
       lkp [obj.otherid] = obj;
       return lkp;
   }, {}); 

   let sorted = []; 

   for (var i=0; i < arr.length; i++) {
      let obj = arr [i];
      if (!!~sorted.indexOf (obj)) continue;
      
      if (otherids [obj.id]) {
          sorted.push (obj) 
          sorted.push(otherids[obj.id])
      }
   }

   return sorted.concat (arr.filter (obj => !~sorted.indexOf (obj)));
}

let sorted = sort (arr);
console.log (sorted);
<script>var arr=[{age:"20",group:"XXX",id:"3L1aa1558002753379",menu:"standard",name:"Adam"},{age:"22",group:"XXX",id:"xhNt11558002753379",menu:"standard",name:"Ola"},{otherid:"3L1aa1558002753379",age:"25",group:"YYY",id:"6ryVK1558002753379",menu:"standard",name:"Wommman"},{otherid:"xhNt11558002753379",age:"25",group:"YYY",id:"aL1aa1558002753312",menu:"standard",name:"xxxxxy"},{age:"25",group:"YYY",id:"6ryVK1558002753379",menu:"standard",name:"xxxxxo"},{otherid:"1ryVK1558002753372",age:"25",group:"YYY",id:"9ryVK155a002753370",
menu:"standard",name:"xxxxxo"},{age:"25",group:"YYY",id:"1ryVK1558002753372",menu:"standard",name:"xxxxxo"}];</script>