基于if语句对第二组数组?

时间:2017-07-19 16:43:13

标签: javascript arrays grouping

非常绿色的编程。

我有两个数组,一个有数字,另一个有文本。

var time = [20,40,60,30,36];
var name = ["jon","tim","jon","jon","andy"];

我想做的是:

如果name == jon取所有相应的数字(时间)并将它们分组。所以它需要名称[0]和时间[0]并将它分组以及jon [2]和时间[2]并将它放在同一组中,依此类推。所以我假设我需要创建新数组,但我不知道如何在没有实际指定数组中的位置的情况下创建if语句。

6 个答案:

答案 0 :(得分:2)

如果要查找数组作为结果集,可以使用哈希表作为结果集的索引。



var time = [20, 40, 60, 30, 36],
    names = ["jon", "tim", "jon", "jon", "andy"],
    groups = {},
    result = [];
    
names.forEach(function (n, i) {
    if (!(n in groups)) {
        groups[n] = result.push([n, 0]) - 1;
    }
    result[groups[n]][1] += time[i];
});

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:1)

循环显示您的姓名并使用相同的索引来获取时间。将它们放入一个新对象,如果该名称尚未使用,则将起始值设置为0.



var arr_time = [20,40,60,30,36];
var arr_name = ["jon","tim","jon","jon","andy"];

var rst = {};

arr_name.forEach(function( person, index ){
	rst[ person] = rst[ person] || 0;
    rst[ person] += arr_time[ index ];
});

console.log( rst );




答案 2 :(得分:1)

您可以使用Array#reduce。

对项目进行分组

如果你想要总和:

var time = [20,40,60,30,36];
var names = ["jon","tim","jon","jon","andy"];

var result = names.reduce(function(o, name, i) {  
  o[name] = (o[name] || 0) + time[i];
  
  return o;
}, {});

console.log(result);

如果您想将它们分组在数组中:

var time = [20,40,60,30,36];
var names = ["jon","tim","jon","jon","andy"];

var result = names.reduce(function(o, n, i) {  
  o[n] = o[n] || [];
  
  o[n].push(time[i]);
  
  return o;
}, {});

console.log(result);

答案 3 :(得分:1)

您可以使用Array#reduce

获得所需的结果



let time = [20, 40, 60, 30, 36];
let array = ["jon", "tim", "jon", "jon", "andy"];

const check = (name, arr) => {
  return { [name]: arr.reduce((s, a, i) => {
    a == name ? s.push(time[i]) : null;
    return s;
  }, [])};
}

console.log(check('jon', array));
console.log(check('tim', array));

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 4 :(得分:1)

   var time = [20,40,60,30,36];
   var names = ["jon","tim","jon","jon","andy"];
   var final = names.reduce(function(a, b, i) { 
                if(!(a.hasOwnProperty(b))) {
                     a[b] = 0;
                }
                 a[b] = a[b] + time[i];
                 return a;
              }, {});

Reduce检查减少的工作方式,它可以帮助您更好地理解。

希望它可以帮到你!

答案 5 :(得分:1)

这是使用for循环的解决方案。对于result数组中的每个索引,我正在检查name对象是否具有该名称。如果它有name我正在添加与该索引相对应的时间,否则我将result添加到该time对象并添加const times = [20,40,60,30,36]; const names = ["jon","tim","jon","jon","andy"]; var result = {}; for(var i = 0 ; i < names.length ; ++i){ let name = names[i]; let time = times[i]; if(result[name] !== undefined){ result[name].push(time); } else { result[name] = [time]; } } console.log(result);

&#13;
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
reduce
&#13;
&#13;
&#13;

以下是使用数组var times = [20,40,60,30,36]; var names = ["jon","tim","jon","jon","andy"]; var result = names.reduce((obj, name, index) => { obj[name] = obj[name] || []; obj[name].push(times[index]); return obj; },{}); console.log(result);的解决方案。

&#13;
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
{{1}}
&#13;
&#13;
&#13;