如何将新对象中的字段值设置为等于数组中字段的最高值?

时间:2013-08-10 08:12:26

标签: javascript arrays

我有一个包含objectiveDetail对象的javascript数组。

objectiveDetails[]

我在这里添加一个新的objectiveDetail:

var emptyObjectiveDetail = {
  "number": formData.objectiveDetails.length +1, // I want to change this !
  "text": "", 
  "response": false, 
  "objectiveId": formData.objectiveId
};
objectiveDetails.push(emptyObjectiveDetail);

我怎样才能使我分配给emptyObjectDetail字段的数字等于数组中的objectiveDetail对象的最大数字当前使用

换句话说,如果我有三个objectiveDetail对象,其中“number”字段设置为1,4和5,那么当我添加一个新对象时,我想用数字6而不是数组的长度添加它+ 1

我也有兴趣使用_underscore,并想知道是否有一个很好的方法来使用下划线以及javascript

5 个答案:

答案 0 :(得分:2)

尝试下面的示例。

基本思想是使用number函数创建一个仅包含objectiveDetails数组的.map值的虚拟单维数组,然后使用Math.max查找最大值在那个虚拟1D阵列中。

var maxNum  = Math.max.apply(Math,objectiveDetails.map(function(o){return o.number;}));
var emptyObjectiveDetail = {
    "number": maxNum+1,
  "text": "", 
  "response": false, 
  "objectiveId": 123
};
objectiveDetails.push(emptyObjectiveDetail);

答案 1 :(得分:1)

如果我理解正确的问题且formData.objectiveDetails仅包含数值,我认为您可以使用Math.max来确定最大值:

var emptyObjectiveDetail = {
  "number": Math.max.apply(null,formData.objectiveDetails) + 1,
  // etc.
};

答案 2 :(得分:0)

你可以尝试

var emptyObjectiveDetail = {
  "number": objectiveDetails[objectiveDetails.length-1].number + 1, // I want to change this !
  "text": "", 
  "response": false, 
  "objectiveId": formData.objectiveId
};
objectiveDetails.push(emptyObjectiveDetail);

它将使键'数字的值等于输入的最后一个数字。

要真正找到最大的,你必须遍历所有的值并找到最大的'数字'。

答案 3 :(得分:0)

这是我的建议:

// create an empty array
var objectiveDetails = [];
// set the current highestNumber to 0
objectiveDetails.highestNumber = 0;
// add a custom method to add an objectiveDetail
objectiveDetails.add = function(objectiveDetail) {
    // if objectiveDetail is not given make an empty object
    objectiveDetail = objectiveDetail ? objectiveDetail : {};
    // if objectiveDetail.number is not given give it highestNumber+1
    if (!objectiveDetail.number) {
        objectiveDetail.number = objectiveDetails.highestNumber + 1;
    }
    // check if highestNumber is still the highest
    if (objectiveDetail.number > objectiveDetails.highestNumber) {
        objectiveDetails.highestNumber = objectiveDetail.number;
    }
    // add objectiveDetail to array
    objectiveDetails.push(objectiveDetail);
}

的示例:

var emptyObjectiveDetail = {
  "number": 3,
  "text": "", 
  "response": false, 
  "objectiveId": 412
};
objectiveDetails.add(emptyObjectiveDetail);

var emptyObjectiveDetail = {
  "number": 2,
  "text": "", 
  "response": false, 
  "objectiveId": 413
};
objectiveDetails.add(emptyObjectiveDetail);

var emptyObjectiveDetail = {
  "text": "", 
  "response": false, 
  "objectiveId": 414
};
objectiveDetails.add(emptyObjectiveDetail);

var emptyObjectiveDetail = {
  "number": objectiveDetails.highestNumber+1,
  "text": "", 
  "response": false, 
  "objectiveId": 414
};
objectiveDetails.add(emptyObjectiveDetail);

console.log(objectiveDetails);

答案 4 :(得分:0)

我相信除了已经建议的方式之外还有其他两种方式:

1)(效率最高):将值存储在一个数字中(可能是全局变量或带有类的变量)。在添加对象时不断更新它。添加新对象时,请检查此编号并相应地添加对象。

优势:在计算方面效率最高。添加新对象时无需浏览所有对象。

2)(优化我相信):好的,这是使用下划线,我相信它是优化:D

var maxNum = _.max(objectiveDetails,function(objectiveDetail){return objectiveDetail.number;});
var emptyObjectiveDetail = {
    "number": maxNum+1,
  "text": "", 
  "response": false, 
  "objectiveId": 123
};
objectiveDetails.push(emptyObjectiveDetail);

优势:使用已针对此类内容进行了优化的库。

希望有所帮助:)