鍍金池/ 問答/HTML5  Java  HTML/ 自定義數(shù)組怎么關(guān)聯(lián)數(shù)據(jù)數(shù)組實現(xiàn)同步排序,,已解決不知道還能不能優(yōu)化代碼實現(xiàn)在評論

自定義數(shù)組怎么關(guān)聯(lián)數(shù)據(jù)數(shù)組實現(xiàn)同步排序,,已解決不知道還能不能優(yōu)化代碼實現(xiàn)在評論中

1.自定義數(shù)組怎么關(guān)聯(lián)數(shù)據(jù)數(shù)組實現(xiàn)同步排序:

2.這是排序前的數(shù)據(jù)數(shù)組:
[null, null, null, 2015-11-23 15:26:57, 2015-11-23 15:3:23, null, null, 2015-11-23 15:16:8, 2015-11-23 15:26:59, null]

按順序?qū)?yīng)自定義字段名數(shù)組:
this.dataNames = ['采樣','送檢','簽收','已登記','IOM','離心','去蓋','測試中','已上傳','已送檢']

3.取數(shù)據(jù)數(shù)組的分鐘給數(shù)據(jù)數(shù)組排序后數(shù)組效果是這樣的:
[0, 0, 0, 0, 0, 0, 3, 16, 26, 26]

4.ECharts中的柱狀圖字段名數(shù)組和數(shù)據(jù)數(shù)組插入只能分開插入:
yAxis: {

        type: 'category',
        data: this.dataNames,

        axisTick: {
            alignWithLabel: true,
        }
    },
    series: [
        {
            name: '2011年',
            type: 'bar',
            data:  this._rowsData
        }
    ]
    
    

5.問題:我怎么實現(xiàn)自定義的字段名數(shù)組跟隨數(shù)值數(shù)組同步排序????
這是交互模塊源碼:
// 交互模塊
SampleTime.prototype.getSampleStateBySampleNo = function (){

//直接調(diào)用Procedure存儲過程
var Request = Global.NewMessage();
//console.log(Request)
Request.ProcedureName = "[proc_datalink_getsample_module]";
Request.ParameterNames = ["SampleNo"];
Request.ParameterDataTypes = ["int"];
Request.ParameterValues = [1];


//獲取數(shù)據(jù)
//PostMessage 異步需回調(diào)
var Return = Global.SendMessage("DB_CallProcedure", Request);
var Returns =  Return.Table0;
console.log(Returns)
//獲取值
this._rowsData = [];
this._rowsData = Returns._rowsData[0];
var datas = [];
var times = [];
for (var data=0;data<this._rowsData.length;data++){
    times.push(this._rowsData[data]);
    datas.push(new Date(this._rowsData[data]).getMinutes());
}

//排序,格式化分鐘
var compare = function (x,y) {//比較函數(shù)
    if (x < y) {
        return -1;
    } else if (x > y) {
        return 1;
    } else {
        return 0;
    }
}
datas.sort(compare);
this._rowsData = datas;
console.log(this._rowsData)
//完整時間
this._rowsDatas = times;



//自定義字段名
this.dataNames = ['采樣','送檢','簽收','已登記',
    'IOM','離心','去蓋','測試中','已上傳','已送檢']

}
SampleTime.prototype.getSampleStateBySampleNo();

6.這是后臺的數(shù)據(jù)格式:
圖片描述

7.這是效果圖,由0開始如果排序正確的話應(yīng)該是3,4,7,8有數(shù)據(jù),自定義字段對應(yīng)的應(yīng)該是:已登記,IOM,測試中,已上傳這幾項有值,但是現(xiàn)在目前貌似所有效果都是值對應(yīng)上了最后4項字段名:
圖片描述

回答
編輯回答
網(wǎng)妓

其實我看了半天,沒有理解你要想問什么,特別是你所謂的“自定義的字段名數(shù)組跟隨數(shù)值數(shù)組同步排序” 到底是什么,即你應(yīng)該以數(shù)據(jù)表達(dá)你排序前的情況和排序后的情況,以及排序的依據(jù)。

2017年8月11日 14:33
編輯回答
墨小羽

存成對象數(shù)組

[
    {
        date,
        type,
    }
]

然后對數(shù)組中的date排序,最后分成兩個數(shù)組

補(bǔ)充一下。實際應(yīng)用還要考慮其他因素,比如后端返回的數(shù)據(jù)情況,數(shù)據(jù)的量級,代碼的可讀性和可維護(hù)性等。

=================

根據(jù)你的代碼做的修改,你中間有些步驟寫的有點冗余我改了下
ES5:

var dateData = this._rowsData = [null, null, null, '2015-11-23 15:26:57', '2015-11-23 15:3:23', null, null, '2015-11-23 15:16:8', '2015-11-23 15:26:59', null]
var typeData = this.dataNames = ['采樣', '送檢', '簽收', '已登記',
  'IOM', '離心', '去蓋', '測試中', '已上傳', '已送檢'
]

var result = this.dataNames
  .map(function (type, index) {               // 拼對象數(shù)組
    return {
      type,
      date: new Date(dateData[index]).getMinutes(),
    }
  })
  .sort(function (a, b) {                     // 排序

    // sort 不穩(wěn)定
    if (a.type === null && b.type === null) {
      return typeData.indexOf(a.type) - typeData.indexOf(b.type)
    }
    return new Date(a.date) - new Date(b.date)
  })
  .reduce(function (result, item) {           // 分割對象數(shù)組為兩個數(shù)組
    return {
      date: result.date.concat(item.date),
      type: result.type.concat(item.type),
    }
  }, { date: [], type: [] })

var date = result.date;
var type = result.type;

console.log(date, type)
// [0, 0, 0, 0, 0, 0, 3, 16, 26, 26]
// ["采樣", "送檢", "簽收", "離心", "去蓋", "已送檢", "IOM", "測試中", "已登記", "已上傳"]

ES6:

const dateData = this._rowsData = [null, null, null, '2015-11-23 15:26:57', '2015-11-23 15:3:23', null, null, '2015-11-23 15:16:8', '2015-11-23 15:26:59', null]
const typeData = this.dataNames = ['采樣', '送檢', '簽收', '已登記',
  'IOM', '離心', '去蓋', '測試中', '已上傳', '已送檢'
]

const { date, type } = this.dataNames
  .map((type, index) => ({ type, date: new Date(this._rowsData[index]).getMinutes() })) // 拼對象數(shù)組
  .sort(({ date: dateA, type: typeA }, { date: dateB, type: typeB }) =>                 // 排序

  // sort不穩(wěn)定
    (typeA === null && typeB === null)
      ? typeData.indexOf(typeA) - typeData.indexOf(typeB)
      : new Date(dateA) - new Date(dateB))
  .reduce((result, { date, type }) => ({                                                // 分割對象數(shù)組為兩個數(shù)組
    date: [...result.date, date],
    type: [...result.type, type]
  }), { date: [], type: [] })

console.log(date, type)
// [0, 0, 0, 0, 0, 0, 3, 16, 26, 26]
// ["采樣", "送檢", "簽收", "離心", "去蓋", "已送檢", "IOM", "測試中", "已登記", "已上傳"]
2017年7月6日 10:51
編輯回答
茍活

謝謝大家,我解決了思路是二維數(shù)組排序,我把我實現(xiàn)代碼貼出來,謝謝幫助我的朋友們:
1.交互模塊:

// 交互模塊
SampleTime.prototype.getSampleStateBySampleNo = function () {

//直接調(diào)用Procedure存儲過程
var Request = Global.NewMessage();
Request.ProcedureName = "[proc_datalink_getsample_module]";
Request.ParameterNames = ["SampleNo"];
Request.ParameterDataTypes = ["int"];
Request.ParameterValues = [1];
//獲取數(shù)據(jù)
//PostMessage 異步需回調(diào)
var Return = Global.SendMessage("DB_CallProcedure", Request);
var Returns = Return.Table0;
console.log(Returns)
// //獲取值
this._rowsData = [];
this._rowsData = Returns._rowsData[0];
var datas = [];
var times = [];
for (var data = 0; data < this._rowsData.length; data++) {
    times.push(this._rowsData[data]);
    datas.push(new Date(this._rowsData[data]).getMinutes());
}
this._rowsData = datas;
console.log(this._rowsData)

//自定義數(shù)組:["采樣","送檢","簽收","已登記","IOM","離心","去蓋","測試中","已上傳","已送檢"]
//混合數(shù)組
this.dataValues = [
    ["采樣",this._rowsData[0]],
    ["送檢",this._rowsData[1]],
    ["簽收",this._rowsData[2]],
    ["已登記",this._rowsData[3]],
    ["IOM",this._rowsData[4]],
    ["離心",this._rowsData[5]],
    ["去蓋",this._rowsData[6]],
    ["測試中",this._rowsData[7]],
    ["已上傳",this._rowsData[8]],
    ["已送檢",this._rowsData[9]]
]
console.log(this.dataValues)
//給二維數(shù)組第二列排序
this.dataValues.sort(function(a,b){
    return a[1]-b[1];
});

this._values = [];
this._datas = [];
for(var i=0;i<this.dataValues.length;i++){
    this._values.push(this.dataValues[i][0]);
    this._datas.push(this.dataValues[i][1]);
}



2.ECharts插入代碼:

yAxis: {

        type: 'category',
        data: this._values ,
        axisTick: {
            alignWithLabel: true,
        }
    },
    series: [
        {
            name: '2011年',
            type: 'bar',
            data: this._datas,
        }
2018年1月21日 03:23
編輯回答
做不到
var dateData1 = [null, null, null, '2015-11-23 15:26:57', '2015-11-23 15:3:23', null, null, '2015-11-23 15:16:8', '2015-11-23 15:26:59', null]
        var typeData1 = ['采樣', '送檢', '簽收', '已登記',
            'IOM', '離心', '去蓋', '測試中', '已上傳', '已送檢'
        ];

        var dateData1IndexObj=new Array(dateData1.length);
        var resultData={
            date:[],
            type:[]
        };
        dateData1.forEach(function(item,index){
            dateData1IndexObj[index]={
                minutes:item?new Date(item).getMinutes():0,
                index:index
            };
        });

        dateData1IndexObj.sort(function(itemA,itemB){
            if(itemA.minutes<itemB.minutes){
                return -1;
            }else if(itemA.minutes>itemB.minutes){
                return 1;
            }else{
                return 0;
            }

        });

        dateData1IndexObj.forEach(function(item,index){
            console.log(typeData1[item.index]);
            resultData.date.push(item.minutes);
            resultData.type.push(typeData1[item.index]);
        });

        console.log(resultData);
2018年2月26日 17:28