如何以相反的顺序迭代JavaScript对象?

如何以相反的顺序迭代JavaScript对象?,javascript,object,javascript-objects,Javascript,Object,Javascript Objects,我有一个嵌套对象,其中包含年份和发布标识符。如何按年份列出出版物,从最新到最早?我读到,从ES6开始,对象的顺序是可预测的。在我的代码中,我有一个函数,它生成一个年份下拉列表(addOption),另一个函数用所选年份或所有年份的出版物(populatePubs)填充div。如果用户选择“所有年份”,则出版物将按从最早到最晚(升序)的顺序显示,但我希望出版物按相反的时间顺序显示。是否有一个内置的JavaScript对象方法可以做到这一点 const testPubs={ 2019 : { ‘1

我有一个嵌套对象,其中包含年份和发布标识符。如何按年份列出出版物,从最新到最早?我读到,从ES6开始,对象的顺序是可预测的。在我的代码中,我有一个函数,它生成一个年份下拉列表(addOption),另一个函数用所选年份或所有年份的出版物(populatePubs)填充div。如果用户选择“所有年份”,则出版物将按从最早到最晚(升序)的顺序显示,但我希望出版物按相反的时间顺序显示。是否有一个内置的JavaScript对象方法可以做到这一点

const testPubs={
2019 : {
‘19-6N’:{
“作者”:“金波”,
“标题”:“测试标题”
},
‘19-7N’:{
“作者”:“吉米”,
“标题”:“测试标题2”
}
},
2018 : {
‘18-6N’:{
“作者”:“乔伊”,
“标题”:“测试标题”
},
‘18-7N’:{
“作者”:“杰菲”,
“标题”:“测试标题2”
}
},
2017 : {
‘17-6N’:{
“作者”:“鲍勃”,
“标题”:“测试标题”
},
‘17-7N’:{
“作者”:“博比”,
“标题”:“测试标题2”
}
},
}
常数startYear=1984;
const currentYear=新日期().getFullYear();
函数addOption(){//将年份添加到下拉菜单中
const select=document.querySelector(“#selectYears”);
让blank_option=document.createElement(“option”);
空白_选项.setAttribute(“选定”,为真);
空白_选项.setAttribute(“禁用”,真);
blank_option.textContent=`Year`;
选择.appendChild(空白选项);
for(设i=currentYear;i>=startYear;i--){
让选项=i;
设el=document.createElement(“选项”);
el.textContent=选项;
选择。追加子对象(el);
}
const allYears=document.createElement(“选项”);
allYears.textContent=`All Years`;
select.appendChild(所有年份)
}
addOption();//添加年份下拉列表
函数populatePubs(年){
const pubsForYear=document.querySelector(“pubsbyear”);
publisbyyear.class=年份;
while(pubsForYear.第一个孩子){
pubsbyear.removeChild(pubsbyear.firstChild);
}
if(测试酒吧中的年份){
对于(让pub在testPubs[year]){//添加指定年份的发布
让doc=document.createElement(“div”);
doc.id=pub;
doc.textContent=`${testPubs[year][pub]。作者}-${testPubs[year][pub]。标题}-${pub}`;
PubsByEar.appendChild(文档);
}
}否则,如果(年份==='All Years'){//需要确保对象的顺序是递减的
对于(让yr进入testPubs){//添加所有年份的出版物
const yearHeader=document.createElement(“h4”);
yearHeader.innerText=yr;
PubsByEar.appendChild(年头);
if(testPubs的类型[yr]=“对象”){
for(让pub进入testpub[yr]){
publisbyyear.class=`所有年份';
const doc=document.createElement(“div”);
doc.id=testPubs[yr][pub];
doc.textContent=`${testPubs[yr][pub]。作者}-${testPubs[yr][pub]。标题}-${pub}`;
PubsByEar.appendChild(文档);
}
}
}
}
}

按年度划分的测试出版物

您可以将数组上的内置反向方法与Object.keys方法结合使用:

function populatePubs(year) {
  const pubsForYear = document.querySelector("#pubsByYear");
  pubsByYear.class = year;
  while (pubsForYear.firstChild) {
    pubsByYear.removeChild(pubsByYear.firstChild);
  }
  if (year in testPubs) {
    for (let pub in testPubs[year]) { // add publications for specified year
      let doc = document.createElement("div");
      doc.id = pub;
      doc.textContent = `${testPubs[year][pub].author} - ${testPubs[year][pub].title} - ${pub}`;
      pubsByYear.appendChild(doc);
    }
  } else if (year === 'All Years') { // need to ensure order of object is descending
    const years = Object.keys(testPubs).reverse();
    for (let yr of years) { // add publications for all years
      const yearHeader = document.createElement("h4");
      yearHeader.innerText = yr;
      pubsByYear.appendChild(yearHeader);
      if (typeof testPubs[yr] === 'object') {
        for (let pub in testPubs[yr]) {
          pubsByYear.class = `All Years`;
          const doc = document.createElement("div");
          doc.id = testPubs[yr][pub];
          doc.textContent = `${testPubs[yr][pub].author} - ${testPubs[yr][pub].title} - ${pub}`;
          pubsByYear.appendChild(doc);
        }
      }
    }
  }
}

看起来这里已经回答了这个问题:虽然这是一种旧的postvar-reversit=Object.entries(testpub.reverse())