如何从数组中获取个人ID和通过/失败次数?

时间:2019-03-26 12:42:34

标签: javascript json angular

这是我拥有的一组数据。我需要一个具有唯一ID和单个通过/失败状态编号的新阵列。我正在使用角度6。

[
{"id":"2","status":"Passed"},
{"id":"5","status":"Passed"},
{"id":"7","status":"Passed"},
{"id":"2","status":"Passed"},
{"id":"9","status":"Passed"},
{"id":"0","status":"Passed"},
{"id":"10","status":"Passed"},
{"id":"4","status":"Passed"},
{"id":"8","status":"Passed"},
{"id":"5","status":"Passed"},
{"id":"7","status":"Passed"},
{"id":"0","status":"Passed"},
{"id":"1","status":"Passed"},
{"id":"0","status":"Passed"},
{"id":"1","status":"Passed"},
{"id":"0","status":"Passed"},
{"id":"11","status":"Passed"},
{"id":"4","status":"Passed"},
{"id":"2","status":"Passed"},
{"id":"5","status":"Passed"},
{"id":"2","status":"Passed"},
{"id":"5","status":"Passed"},
{"id":"2","status":"Passed"},
{"id":"5","status":"Failed"}
]

//JavaScript code

我希望输出

[
{"id":"1", "Passed": "2", "Failed": "0"},
{"id":"2", "Passed": "5", "Failed": "0"}....and so on]

输出中应该有10个元素。

4 个答案:

答案 0 :(得分:0)

您可以使用 import * as _ from 'lodash'

let passItem = _.find(array, function (o) { return o.status == 'passed' }) 
let failItem = _.find(array, function (o) { return o.status == 'fail' }) 

答案 1 :(得分:0)

您可以尝试这些方式。

SOLUTION

只需说您的{id,“ Passed”,“ Failed”}有一个类,它就可以计算“ Passed”和“ Failed”之后的整数。

class PassFailCounter {
    constructor(id) {
        this.id = id;
        this.Passed = 0;
        this.Failed = 0;
    }

    addPass() {
        this.Passed++;
    }

    addFail() {
        this.Failed++;
    }
}

只需读取数据并在Hashmap中记住它,就意味着id是唯一的。 之后,JSON.stringify()返回所述地图的.values(),您将获得您提到的确切输出。

let str = '[{"id":"2","status":"Passed"},{"id":"5","status":"Passed"},{"id":"7","status":"Passed"},{"id":"2","status":"Passed"},{"id":"9","status":"Passed"},{"id":"0","status":"Passed"},{"id":"10","status":"Passed"},{"id":"4","status":"Passed"},{"id":"8","status":"Passed"},{"id":"5","status":"Passed"},{"id":"7","status":"Passed"},{"id":"0","status":"Passed"},{"id":"1","status":"Passed"},{"id":"0","status":"Passed"},{"id":"1","status":"Passed"},{"id":"0","status":"Passed"},{"id":"11","status":"Passed"},{"id":"4","status":"Passed"},{"id":"2","status":"Passed"},{"id":"5","status":"Passed"},{"id":"2","status":"Passed"},{"id":"5","status":"Passed"},{"id":"2","status":"Passed"},{"id":"5","status":"Failed"}]';

let data = JSON.parse(str);
let map = new Map();

data.forEach(entry => {
    let exist = map.get(Number(entry.id));
    if(!exist) {
        map.set(Number(entry.id), new PassFailCounter(Number(entry.id)));
        exist = map.get(Number(entry.id));
  }
  entry.status == "Passed" ? exist.addPass() : exist.addFail()      
});

console.log(JSON.stringify(Array.from(map.values())));

也许不是最短的解决方案,但它可行。

编辑: 代替

JSON.stringify(Array.from(map.values()))

您也可以

JSON.stringify(Array.from(map.values()), function(key, value) {
    if(isNaN(value)) {
      return value;
    }
    return value.toString();
})

如果字符串类型对输出很重要。

答案 2 :(得分:0)

尝试一下,只有一个简单的迭代:

let str = '[{"id":"2","status":"Passed"},{"id":"5","status":"Passed"},{"id":"7","status":"Passed"},{"id":"2","status":"Passed"},{"id":"9","status":"Passed"},{"id":"0","status":"Passed"},{"id":"10","status":"Passed"},{"id":"4","status":"Passed"},{"id":"8","status":"Passed"},{"id":"5","status":"Passed"},{"id":"7","status":"Passed"},{"id":"0","status":"Passed"},{"id":"1","status":"Passed"},{"id":"0","status":"Passed"},{"id":"1","status":"Passed"},{"id":"0","status":"Passed"},{"id":"11","status":"Passed"},{"id":"4","status":"Passed"},{"id":"2","status":"Passed"},{"id":"5","status":"Passed"},{"id":"2","status":"Passed"},{"id":"5","status":"Passed"},{"id":"2","status":"Passed"},{"id":"5","status":"Failed"}]';

let data = JSON.parse(str);

let newData = {};

data.forEach(item => {

  // initial data if key doesn't exists
  if ( ! newData[item.id]) {
    newData[item.id] = { Passed:0, Failed:0 }; 
  } 

  // increment Failed or Passed value
  newData[item.id][item.status]++; 
});

答案 3 :(得分:0)

这样做会更容易:

const results = [];
data.forEach(item => {
   const foundItem = results.find(result => result.id === item.id);
   if (!foundItem) {
      results.push({ 
         id: item.id, 
         Passed: (item.status === 'Passed') ? 1 : 0, 
         Failed: (item.status === 'Failed') ? 1 : 0
      });
   } else {
      if (item.status === 'Passed') foundItem.Passed += 1;
      else if (item.status === 'Failed') foundItem.Failed += 1;
   }
});