数组结合重复值

时间:2017-05-16 12:45:15

标签: javascript arrays extjs

我有以下数组:

Object { id: "1", name: "a", hour: "08:00:00" }  
Object { id: "2", name: "b", hour: "08:00:00" }  
Object { id: "3", name: "c", hour: "08:00:00" }  
Object { id: "4", name: "d", hour: "07:15:00" }  
Object { id: "5", name: "e", hour: "08:00:00" }  
Object { id: "6", name: "f", hour: "08:00:00" }  
Object { id: "7", name: "g", hour: "09:00:00" }  
Object { id: "8", name: "h", hour: "08:30:00" }

当我尝试访问" 小时"价值:

for(i = 0; i < array.length; i++){
    var hour = array[i]['hour'];
    console.log(hour);
}

我得到以下结果:

08:00:00  (3 repeats) 
07:15:00   
08:00:00  (2 repeats) 
09:00:00   
08:30:00

我怎样才能避免这种情况并获得:

08:00:00  
08:00:00 
08:00:00 
07:15:00   
08:00:00
08:00:00  
09:00:00   
08:30:00

3 个答案:

答案 0 :(得分:4)

您正在查看正在打印的浏览器...您无法使用js更改此内容。它是一个依赖于浏览器的设置,在Chrome中只是启用时间戳来查看不同行中的每个console.log。

django registration redux

答案 1 :(得分:0)

这是特定于浏览器的。但是你可以通过将数据保存在一个变量中来解决这个问题,在这个变量中用\ n分隔每一行并在结尾处输出到控制台。

   var output='';
   for(i = 0; i < array.length; i++){
        var hour = array[i]['hour'];
        if (i) output +='\n';
        output += hour;
    }
    console.log(output);

答案 2 :(得分:0)

检查console has different methods和用法..但你可以通过遍历数组,使用索引并使用可选的延迟参数{{1}调用WindowOrWorkerGlobalScope.setTimeout()来在控制台中获得所需的输出}:

index * 200
var array = [{ id: "1", name: "a", hour: "08:00:00" }  , { id: "2", name: "b", hour: "08:00:00" }  , { id: "3", name: "c", hour: "08:00:00" }  , { id: "4", name: "d", hour: "07:15:00" }  , { id: "5", name: "e", hour: "08:00:00" }  , { id: "6", name: "f", hour: "08:00:00" }  , { id: "7", name: "g", hour: "09:00:00" }  , { id: "8", name: "h", hour: "08:30:00" }];

array.forEach(function (elem, index) {
  setTimeout(function () {
    console.log(elem.hour);
  }, index * 200);
});