Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 为对象数组中的对象编号_Javascript_Arrays_Ecmascript 6 - Fatal编程技术网

Javascript 为对象数组中的对象编号

Javascript 为对象数组中的对象编号,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,我有一个对象数组: let _arr =[{obj_num: 1.1}, {obj_num: 1.5}, {obj_num: 4}, {obj_num: 3.1}, {obj_num: 3.3}]; 我需要根据小数点的两边对它们的数字进行排序,即: let _arr =[{obj_num: 1.1}, {obj_num: 1.2}, {obj_num: 2}, {obj_num: 3.1}, {obj_num: 3.2}]; 对象按小数点后的第一个数字分组:因此1.1和1.2属于对象组1 对

我有一个对象数组:

let _arr =[{obj_num: 1.1}, {obj_num: 1.5}, {obj_num: 4}, {obj_num: 3.1}, {obj_num: 3.3}];
我需要根据小数点的两边对它们的数字进行排序,即:

let _arr =[{obj_num: 1.1}, {obj_num: 1.2}, {obj_num: 2}, {obj_num: 3.1}, {obj_num: 3.2}];
对象按小数点后的第一个数字分组:因此1.1和1.2属于对象组1

对象编号中没有小数点的对象位于其自己的组中

需要根据对象的组按发送顺序对对象进行编号

因此,如果obj_num:1.1和obj_num:1.3属于第1组,它们应该分别为1.1和1.2

如果obj编号1.1、1.3、2、4.3按顺序排列,它们将需要变成1.1、1.2、2、3.1

我被难住了,认为我是小题大做

我现在正在尝试的是:

this.data.questions =[{question_num: 1.1}, {question_num: 1.5}, {question_num: 4}, {question_num: 3.1}, {question_num: 3.3}];
let _normalQuestionRef = 0;
let _questionRef = {
    totalObjs: _arr.length,
    objects: {},
};

// Create a reference object that groups questions by the number before the decimal
this.data.questions.forEach((_q, _i) => {
    let _qNum = _q.question_num.split('.')[0];
    _questionRef.questions[`question_${_qNum}`] = _questionRef.questions[`question_${_qNum}`] || [];
    _questionRef.questions[`question_${_qNum}`].push({
        index: _i,
        q: _q,
    });
});

// Re-populate the original array
Object.keys(_questionRef.questions).forEach((_qRef) => {
    let _qSet = _questionRef.questions[_qRef];

    // Increment the base question number
    _normalQuestionRef ++;

    // If there are questions with a decimal point number
    if (_qSet.length > 1) {

        // Loop each decimal point and place it in the specified index of the array
        _qSet.forEach((_q, i) => {
            _q.q.question_num = _normalQuestionRef + '.' + String(i + 1);
            this.data.questions[_q.index] = _q;
        });
    } else {
        // Just add it as is
        _qSet[0].q.question_num = _normalQuestionRef;
        this.data.questions[_qSet[0].index] = _qSet[0].q;
    };
});

但它并不完全在那里。任何正确方向的指导都将不胜感激。

您可以在组内保存最后一个值、新组值和偏移量

var数组=[1.1,1.5,4,3.1,3.3], 结果=array.maplast,组,偏移量=>a=>{ 偏移量+=0.1; 如果Math.floorlast!==Math.floora{ 组++; 偏移量=a==Math.floora?0:0.1; } last=a; 返回组+偏移量; }0, 0, 0; console.logresult;
.作为控制台包装{max height:100%!important;top:0;}按小数点两侧排序是什么意思?你的意思是数字?@Jordão-我的错误,我已经更正了答案-为了可读性,对象被截断了。@AndrewLi-与其说是大小写或排序,不如说是根据对象在数据库中的位置重新编号array@AndrewLi考虑已经移除对象的第一个数组,第二个问题是否不够清楚?reduce master再次罢工-感谢这正是我所寻找的。reduce很漂亮,你可以用它来表达几乎任何可以想象的转换。