基于两个字段值javascript计算Json数据

时间:2018-01-06 02:21:35

标签: javascript angularjs json typescript

在页面上加载一个角度组件我做了这样的get请求:

 private getData() {
    const url = 'http...';
    const data= this.http.get(url).map( res => res.json());
    return data;
 }

我得到了这个结果:

[
{
id: 1,
type: "science",
action: "on",
name: "title 1",
frequency: 10,
author: null,
},
{
id: 3,
type: "thriller",
action: "off",
name: "Programming",
frequency: 60,
author: 1515202398191,
},
{
id: 4,
type: "article",
action: "pause",
name: "Stories about life",
frequency: 60,
author: 1515202398191,
},
{
id: 5,
type: "science",
action: "on",
name: "Lorem Ipsum",
frequency: 60,
author: 1515202398191,
},
{
id: 6,
type: "science",
action: "off",
name: "Testing",
frequency: 60,
author: 1515202398191,
},
{
id: 8,
type: "science",
action: "on",
name: "Programming",
frequency: 60,
author: 1515202398191,
}
]

在数组内部,我有不同的项目,所有项目都包含值typeaction。我想要计算typeaction具有相同值的项目,这样我就可以得到一些统计数据:

[{"type":"science", action:"on", count: 3},
 {"type":"science", action:"off", count: 1},
 {"type":"thriller", action:"off", count: 1},
 {"type":"article", action:"pause", count: 1},
]

我开始做点什么了,但我不知道怎么能这样做

stats: any;
 function countArr(array){
      var array= [];
      var count = 0;
      array.forEach( function(currentItem){

         array.forEach( function(item){
         // check if 
         if(item.type == currentItem.type && item.action == currentItem.action){
                count++;
             }
           });
           var itemJson = {'type':currentItem.type, 'action':currentItem.action, 'count':count };  
           array.push(itemJson);
      });

    }

private getData() {
        const url = 'http...';
        const data= this.http.get(path).map( res => res.json());
        this.stats = countArr(data);
        return data;
     }

2 个答案:

答案 0 :(得分:0)

试试这个例子 -



Shadows.extract()




等效的打字稿 -

var data = [
    {
        id: 1,
        type: "science",
        action: "on",
        name: "title 1",
        frequency: 10,
        author: null,
    },
    {
        id: 3,
        type: "thriller",
        action: "off",
        name: "Programming",
        frequency: 60,
        author: 1515202398191,
    },
    {
        id: 4,
        type: "article",
        action: "pause",
        name: "Stories about life",
        frequency: 60,
        author: 1515202398191,
    },
    {
        id: 5,
        type: "science",
        action: "on",
        name: "Lorem Ipsum",
        frequency: 60,
        author: 1515202398191,
    },
    {
        id: 6,
        type: "science",
        action: "off",
        name: "Testing",
        frequency: 60,
        author: 1515202398191,
    },
    {
        id: 8,
        type: "science",
        action: "on",
        name: "Programming",
        frequency: 60,
        author: 1515202398191,
    }
];
function getCount() {
    var countData = [];
    data.forEach(function (item, index) {
        var existingItem = countData.find(function (countItem) {
            return item.type === countItem.type &&
                item.action === countItem.action;
        });
        if (existingItem) {
            existingItem.count++;
        }
        else {
            existingItem = {
                type: item.type,
                action: item.action,
                count: 1
            };
            countData.push(existingItem);
        }
    });
    return countData;
}
console.log(getCount());

答案 1 :(得分:-1)

你可以这样做:

var res = []
// data is your initial array
data.forEach((item) => {
    const result = res.find(x => x.type === item.type && x.action === item.action)
    if (result) result.count += 1
    else res.push({type: item.type, action: item.action, count: 1})
})