Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJs对象排序_Javascript_Arrays_Angularjs_Json_Sorting - Fatal编程技术网

Javascript AngularJs对象排序

Javascript AngularJs对象排序,javascript,arrays,angularjs,json,sorting,Javascript,Arrays,Angularjs,Json,Sorting,在主数组对象中有这样的JSON obj = [{{"title":"1-Introduction"}, {"title":"5-Introduction"}, {"title":"20-Introduction"}, {"title":"4-Introduction"} }] 我想对上面的对象进行如下排序 obj = [{{"title":"1-Introduction"}, {"title":"4-Introduction"},

在主数组对象中有这样的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);
        }
我想根据前面的标题数字对所有项目进行排序,例如字符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"
          }

您可以对该字符进行
拆分
,并通过该字符进行排序(假设您的主数据结构名为
结构

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

只需提取这些数值,让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>
{{item.title}
orderBy还为asc/desc排序接受第二个参数(true/false)

您可以将a与简单的
排序
函数一起使用,在该函数中,您可以将数组中每个对象的
标题
值解析为int并进行比较。类似如下:

var-arr=[{
“标题”:“1-导言”
},
{
“标题”:“5-导言”
},
{
“标题”:“20导言”
},
{
“标题”:“4-导言”
}
];
arr.sort(函数(a,b){
const aVal=parseInt(a.title.split(“-”[0]);
const bVal=parseInt(b.title.split(“-”[0]);
返回aVal-bVal;
});

console.log(arr);
为了简单起见,我只在Demo中包含了对象的一部分。上面的回答解决了我的问题,但我也尝试了您的方法,并且出现了一个错误。标记“orderBy”是表达式[container.items orderBy:getNumber(item):true]第17列的意外标记,起始于[orderBy:getNumber(item):true]。很可能您复制了错误的内容,请将您的代码与plnkr演示中提供的示例进行比较。此外,如果您可以创建plunker,我可以帮助您进行调试。是的,我刚刚看到了示例,现在它也在工作。很高兴我能够提供帮助。感谢您的回答,我将检查并让您知道
<div ng-repeat="item in data | orderBy:getNumber:false">{{item.title}}</div>