如何将.reduce函数生成的数组操作为单独的键/值对? (JavaScript的)

时间:2016-09-01 09:29:39

标签: javascript arrays

我正在努力解决我遇到的问题.reduce;

目前,我正在迭代一个数组来计算重复数量,如下所示;

var data = ['141:150.5', '141:150.5', '141:150.5', '224:297.5','83:351.5','278:434.5','332:182', '332:182','263:135','256:155','256:155','105:65']


var reducer = function(tally, vote) {
  if(!tally[vote]){
    tally[vote] = 1
  } else {
    tally[vote] = tally[vote] + 1;
  }
  
  return tally;
}

var result = data.reduce(reducer, {});

console.log(result)

我想要做的是将每个项目的输出从这个数组拆分成一个有3个键/值对的对象。

所以从("141:150.5": 3)上面的输出中取出第一个结果我想生成以下内容;

{ 
value1: 141,
value2: 150.5,
count: 3
},

等等。

但是由于上面的函数没有输出数组长度,这意味着我无法遍历result以开始构建所需的对象。

我在这里有点失落 - 非常感谢任何帮助/建议!

7 个答案:

答案 0 :(得分:3)

如评论所示,您只需要分割key值并相应地处理。

另外,我已经休闲地更新你的缩减功能。

var data = ['141:150.5', '141:150.5', '141:150.5', '224:297.5', '83:351.5', '278:434.5', '332:182', '332:182', '263:135', '256:155', '256:155', '105:65']

var reducer = function(tally, vote) {
  tally[vote] = (tally[vote] || 0) + 1;
  return tally;
}

var result = data.reduce(reducer, {});

var output = [];
for(var k in result){
  var _t = k.split(":");
  output.push({
    value1: _t[0],
    value2: _t[1],
    count: result[k]
  });
}

console.log(output)

您还可以更新reduce函数以直接输出如下内容:

var data = ['141:150.5', '141:150.5', '141:150.5', '224:297.5', '83:351.5', '278:434.5', '332:182', '332:182', '263:135', '256:155', '256:155', '105:65']

var count = 1;
var reducer = function(p, c, i, a) {
  if (p && p !== c) {
    var _t = p.split(":");
    result.push({
      value1: _t[0],
      value2: _t[1],
      count: count
    });
    count = 1;
    if (i === a.length - 1) {
      _t = c.split(":");
      result.push({
        value1: _t[0],
        value2: _t[1],
        count: count
      });
    }
  } else {
    count++;
  }
  return c
}

var result = [];
data.sort().reduce(reducer);

console.log(result)

答案 1 :(得分:1)

您可以使用for-in循环简单地迭代对象,并使用:将密钥拆分为.split(':')



var data = ['141:150.5', '141:150.5', '141:150.5', '224:297.5', '83:351.5', '278:434.5', '332:182', '332:182', '263:135', '256:155', '256:155', '105:65']

var reducer = function(tally, vote) {
  if (!tally[vote]) {
    tally[vote] = 1
  } else {
    tally[vote] = tally[vote] + 1;
  }

  return tally;
}

var result = data.reduce(reducer, {});

for (const key in result) {
  const value = key.split(':')
  result[key] = {value1: value[0], value2: value[1], count: result[key]}
}

console.log(result)




答案 2 :(得分:1)

你是那里的一部分。



var data = ['141:150.5', '141:150.5', '141:150.5', '224:297.5', '83:351.5', '278:434.5', '332:182', '332:182', '263:135', '256:155', '256:155', '105:65']
var reducer = function (tally, vote) {
    if (!tally[vote]) {
        tally[vote] = 1
    } else {
        tally[vote] = tally[vote] + 1;
    }
    return tally;
}
var result = data.reduce(reducer, {});

var result2 = [];
for (key in result) {
    var a = {};
    a.value1 = Number(key.split(":")[0]);
    a.value2 = Number(key.split(":")[1]);
    a.count = result[key];
    result2.push(a);
}
console.log(result2);




答案 3 :(得分:0)

Object.keys( result ).length会为您提供密钥数量

答案 4 :(得分:0)

var m = [];
_.forEach(result,function(value,key){
         let k = key.split(':');
         m.push({value1:parseInt(k[0]),value2:parseInt(k[1]),count:value});
});
console.log(m)

这里的脚本正是您正在寻找的。它使用Lodash。祝你有美好的一天!

答案 5 :(得分:-1)

Just try the below code:


var data = ['141:150.5', '141:150.5', '141:150.5', '224:297.5', '83:351.5', '278:434.5', '332:182', '332:182', '263:135', '256:155', '256:155', '105:65']

var reducer = function(tally, vote) {
  if (!tally[vote]) {
    tally[vote] = 1
  } else {
    tally[vote] = tally[vote] + 1;
  }

  return tally;
}

var result = data.reduce(reducer, {});

console.log(result);
var resultArr = [];
for(var key in result) {
    var obj = {};
  var splitArr = key.split(":");
  obj.value1 = splitArr[0];
  obj.value2 = splitArr[1];
  obj.count = result[key];
  resultArr.push(obj);
}

console.log(resultArr);

Now if you want to access data you can try like this:
console.log(resultArr[0].value1);
console.log(resultArr[0].value2);
console.log(resultArr[0].count);

答案 6 :(得分:-2)

如果我不误解你想做什么,你可以通过使用in来迭代你的“结果”对象:

for (var prop in result) {
    if (result.hasOwnProperty(prop )) {
        var val = result[prop];
        // then output your desired format
    }
}