如何将缺少的项添加到JavaScript数组中?

时间:2015-03-10 07:33:21

标签: javascript arrays

我有以下JavaScript数组:

[
    {
        "day": 0,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 2,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 3,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 4,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 6,
        "best_hour_of_the_day": "--",
        "appt_success_rate": "0",
        "appt_100_numbers": "0"
    }
]

我已经定义了数组,其中包含了我在结果中需要的日期。

var daysOfTheWeek = [0,1,2,3,4,5,6];

我想问一下,如何将缺少的项添加到JavaScript数组中的好方法是什么?

这意味着添加缺失值1和6以获得以下JavaScript结果:

[
    {
        "day": 0,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 1,
        "best_hour_of_the_day": "--",
        "appt_success_rate": "0",
        "appt_100_numbers": "0"
    },
    {
        "day": 2,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 3,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 4,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 5,
        "best_hour_of_the_day": "10",
        "appt_success_rate": "60",
        "appt_100_numbers": "27"
    },
    {
        "day": 6,
        "best_hour_of_the_day": "--",
        "appt_success_rate": "0",
        "appt_100_numbers": "0"
    }
]

2 个答案:

答案 0 :(得分:1)

我在 JAVASCRIPT 中提供解决方案:

参见演示:

jsfiddle链接: http://jsfiddle.net/3phba87q/

鉴于:

var dummyData=[
        {
            day: 0,
            best_hour_of_the_day: "10",
            appt_success_rate: "60",
            appt_100_numbers: "27"
        },
        {
            day: 2,
            best_hour_of_the_day: "10",
            appt_success_rate: "60",
            appt_100_numbers: "27"
        },
        {
            day: 3,
            best_hour_of_the_day: "10",
            appt_success_rate: "60",
            appt_100_numbers: "27"
        },
        {
            day: 4,
            best_hour_of_the_day: "10",
            appt_success_rate: "60",
            appt_100_numbers: "27"
        },
        {
            day: 5,
            best_hour_of_the_day: "10",
            appt_success_rate: "60",
            appt_100_numbers: "27"
        }
    ];
var daysOfTheWeek = [0,1,2,3,4,5,6];

以获得预期结果:

var existingDays = [];
// get the existing days

for(var i=0;i<dummyData.length;i++){
    existingDays[i] = dummyData[i]['day'];
}

// check which day's data is missing; then create a dummy object and push it to the dummyData object

for(var i=0;i<daysOfTheWeek.length;i++){
   if(existingDays.indexOf(parseInt(daysOfTheWeek[i])) < 0){
       var dummyObject = {
        "day": i,
        "best_hour_of_the_day": "--",
        "appt_success_rate": "0",
        "appt_100_numbers": "0"
       };
       dummyData.push(dummyObject);
   }
}

//sort day wise ascending

dummyData.sort(function(x,y){ return parseInt(x.day) - parseInt(y.day) });

答案 1 :(得分:1)

在现代JS中,这将是

function addMissingItems(days, input) {

  function get (day) { return find(day) || make(day); }
  function find(day) { return input.find(function(x) { return x.day===day; }); }
  function make(day) { return { day: day, ...}; }

  return days.map(get);
}

var newArray = addMissingItems(daysOfTheWeek, input);

这使用map创建并返回与daysOfTheWeek平行的数组,其中每个元素都是调用get的结果。 get尝试通过调用find在当天的输入中查找元素,如果这不起作用,则通过调用make为该日创建新元素。< / p>

这使用Array#find,您的浏览器可能会提供,也可能无法使用。在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find有一个polyfill。或者,你可以写自己的:

function findInArray(array, condition) {
    for (var i = 0; i < array.length; i++) {
        var elt = array[i];
        if (condition(elt, i, array)) return elt;
    }
}

并将上面代码中find的实现更改为

return findInArray(input, function() {...});