【问题标题】:AngularJs object SortingAngularJs 对象排序
【发布时间】:2019-06-12 20:03:54
【问题描述】:

我在主数组对象中有这样的 JSON

obj = [{{"title":"1-Introduction"},
       {"title":"5-Introduction"},
       {"title":"20-Introduction"},
       {"title":"4-Introduction"} }]

我想对上面的对象进行排序

obj = [{{"title":"1-Introduction"},
       {"title":"4-Introduction"},
       {"title":"5-Introduction"},
       {"title":"20-Introduction"} }]

到目前为止我已经尝试过什么

$scope.checkWeightage=function(){
            thisObj.scheduleSubject.map(itm=>itm.sectionRange.map(subItm=>{
                    var min = subItm.chapterActualWeightage-(subItm.chapterActualWeightage/10);
                    var max = subItm.chapterActualWeightage+(subItm.chapterActualWeightage/10);
                    var sum = subItm.items.reduce((total,it)=>{
                       return total+it.actualWeightage;
                    },0);
                    subItm['weightageError'] = (sum>max || sum<min)?true:false;
                    subItm['ChapterActualWeightageCurrently'] = parseFloat(Math.round(sum*100)/100);
                    subItm.items.sort((a,b)=>a.title.split(/_(.+)/)[0]>b.title.split(/_(.+)/)[0]);
                })
            ); console.log(thisObj.scheduleSubject[0].chapterData); //.1[0].title);
            //console.log("CHECK weightage",thisObj.scheduleSubject);
        }

如何在我的主阵列上跟踪标题

alert(thisObj.scheduleSubject[0].sectionRange[0].items[0].title);

我想根据之前的标题数字对所有项目进行排序 - 字符示例 1-、2-、3-、4-、5-、6-、21-、56- 等等。

主数组结构

[
  {
    "id": "25",
    "section": "1",
    "sectionRange": [
      {
        "sectionNumber": 1,
        "effectAllowed": "all",
        "Date": "",
        "items": [
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 283,
            "actualWeightage": 3.42,
            "totalPaperMarks": 132,
            "title": "10-Creation & Registration of Charges",
            "$$hashKey": "object:146"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 284,
            "actualWeightage": 2.23,
            "totalPaperMarks": 132,
            "title": "11-Allotment of Securities & Issue of Certificates",
            "$$hashKey": "object:147"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 285,
            "actualWeightage": 1.37,
            "totalPaperMarks": 132,
            "title": "12-Membership in a Company",
            "$$hashKey": "object:148"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 286,
            "actualWeightage": 3.42,
            "totalPaperMarks": 132,
            "title": "13-Transfer & Transmission of Securities",
            "$$hashKey": "object:149"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 287,
            "actualWeightage": 7.53,
            "totalPaperMarks": 132,
            "title": "14-Institution of Directors",
            "$$hashKey": "object:150"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 288,
            "actualWeightage": 1.37,
            "totalPaperMarks": 132,
            "title": "15-Independent Directors",
            "$$hashKey": "object:151"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 289,
            "actualWeightage": 13.35,
            "totalPaperMarks": 132,
            "title": "16-Board & its Powers",
            "$$hashKey": "object:152"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 290,
            "actualWeightage": 8.22,
            "totalPaperMarks": 132,
            "title": "17-Appointment & Remuneration of Key Managerial Personnel",
            "$$hashKey": "object:153"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 291,
            "actualWeightage": 6.68,
            "totalPaperMarks": 132,
            "title": "18-General Meetings",
            "$$hashKey": "object:154"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 292,
            "actualWeightage": 1.37,
            "totalPaperMarks": 132,
            "title": "19-Loans & Investments by Companies",
            "$$hashKey": "object:155"
          }

【问题讨论】:

    标签: javascript arrays angularjs json sorting


    【解决方案1】:

    您可以在该字符上split 并通过该字符进行排序(假设您的主要数据结构名为structure

    structure.forEach(s => {
        s.sectionRange.forEach(sr => {
            sr.items.sort(function(a, b) {
                let aParts = a.split("-"),
                    bParts = b.split("-");
    
                return +aParts[0] - +bParts[0];
            });
        });
    });
    

    【讨论】:

    • 感谢您的回答,我会检查并通知您
    【解决方案2】:

    只需提取这些数值,然后让 angularjs 的 orderBy 过滤器为您完成这项工作

    JS

      $scope.getNumber = function(row){
        var value = row.title.split("-")[0];
        return parseInt(value);
      };
    

    html

    <div ng-repeat="item in data | orderBy:getNumber:false">{{item.title}}</div>
    

    orderBy 还为 asc / desc 排序采用第二个参数(true / false)

    Demo

    【讨论】:

    • 为简单起见,我只在演示中包含了您的对象的项目部分
    • 以上回答解决了我的问题,但我也尝试了您的方法,但出现错误。标记“orderBy”是表达式 [container.items orderBy:getNumber(item):true] 的第 17 列中的意外标记,从 [orderBy:getNumber(item):true] 开始。
    • 很可能您复制了一些错误,请将您的代码与 plnkr 演示中提供的示例进行比较。另外,如果您可以创建一个 plunker,我可以帮助您进行调试。
    • 是的,我刚刚看到了这个例子,现在它也可以工作了。
    • 很高兴我能提供帮助。
    【解决方案3】:

    您可以将Array.prototype.sort 与一个简单的sort 函数一起使用,在该函数中您将数组中每个对象的title 值解析为int 并进行比较。像这样的:

    var arr = [{
        "title": "1-Introduction"
      },
      {
        "title": "5-Introduction"
      },
      {
        "title": "20-Introduction"
      },
      {
        "title": "4-Introduction"
      }
    ];
    
    
    arr.sort(function(a, b) {
      const aVal = parseInt(a.title.split("-")[0]);
      const bVal = parseInt(b.title.split("-")[0]);
      return aVal - bVal;
    });
    
    console.log(arr);

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多