总结对象数组中的值

时间:2018-04-25 09:25:37

标签: javascript

[
{
    "id": {
    "extId": "112",
    "year": "2000"
    },
    "Count": 1
},
{
    "id": {
    "extId": "113",
    "year": "2001"
    },
    "Count": 446
},
{
    "id": {
    "extId": "115",
    "year": "2000"
    },
    "Count": 742
}, ...
]

我有很长的对象数组。我需要根据年份总结一下。例如,我想要像[{2000:743},{2001:446},...]这样的东西。 我不知道如何在javascript中继续。我应该遍历数组中的每个对象并检查年份,还是有一些javascript函数可以使这更简单。

感谢。

7 个答案:

答案 0 :(得分:2)

您可以使用Array.reduce()

let countByYear = objects.reduce((acc, next) => {
    acc[next.id.year] = (acc[next.id.year] || 0) + next.Count;
    return acc;
}, {});

注意,这会产生与你的例子不同的结构(因为我读得太邋)了):

{
    2000: 743,
    2001: 446
}

但是我会说这比[ { 2000: 743 }, { 2001: 446 } ]更容易使用,因为在这种情况下你有一个对象数组,每个对象都有一个键,你无法知道那个键是什么,我想象的是,很难对它们进行迭代。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

答案 1 :(得分:1)

您可以使用reduce:

arr.reduce((result, current) => { result.push({[current.year]: current.Count}); return result }, [])

这将为您提供此结构[{2000:743},{2001:44}],如果您只需要过滤特定年份,您甚至可以先arr.filter(filterFn)

答案 2 :(得分:1)

您可以使用Map并获取对象数组的键/值。



var data = [{ id: { extId: "112", year: "2000" }, Count: 1 }, { id: { extId: "113", year: "2001" }, Count: 446 }, { id: { extId: "115", year: "2000" }, Count: 742 }],
    count = Array.from(
        data.reduce(
            (m, { id: { year }, Count }) => m.set(year, (m.get(year) || 0) + Count),
            new Map
        ),
        ([year, count]) => ({ [year]: count })
    );
    
console.log(count);

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




答案 3 :(得分:1)

<script>
  var arr=[
{
    "id": {
    "extId": "112",
    "year": "2000"
    },
    "Count": 1
},
{
    "id": {
    "extId": "113",
    "year": "2001"
    },
    "Count": 446
},
{
    "id": {
    "extId": "115",
    "year": "2000"
    },
    "Count": 742
}
];
var result=arr.reduce((result, current) => {
  result.push({[current.id.year]: current.Count});
  return result;
}, []);
console.log(result);

</script>

答案 4 :(得分:1)

ES6

您可以使用 reduce() 功能获取所需的结果。

<强>样本

&#13;
&#13;
const data = [{"id": {"extId": "112","year": "2000"},"Count": 1},{"id": {"extId": "113","year": "2001"},"Count": 446},{"id": {"extId": "115","year": "2000"},"Count": 742}];

let result = data.reduce((r, {Count,id: {year}}) => {
  r[year] = (r[year] || 0) + Count;
  return r;
}, {});

console.log([result])
&#13;
.as-console-wrapper {max-height: 100% !important;top: 0;}
&#13;
&#13;
&#13;

答案 5 :(得分:1)

reduce将为您提供诀窍:

var arr = [
{
    "id": {
    "extId": "112",
    "year": "2000"
    },
    "Count": 1
},
{
    "id": {
    "extId": "113",
    "year": "2001"
    },
    "Count": 446
},
{
    "id": {
    "extId": "115",
    "year": "2000"
    },
    "Count": 742
},
{
    "id": {
    "extId": "116",
    "year": "2001"
    },
    "Count": 44
}
];

let count = arr.reduce((acc, next) => {
    acc[next.id.year] = (acc[next.id.year] || 0) + next.Count;
    return acc;
}, {});

console.log(count);

答案 6 :(得分:0)

var yearCount={};
var temp=[
{
    "id": {
    "extId": "112",
    "year": "2000"
    },
    "Count": 1
},
{
    "id": {
    "extId": "113",
    "year": "2001"
    },
    "Count": 446
},
{
    "id": {
    "extId": "115",
    "year": "2000"
    },
    "Count": 742
}
];
temp.forEach(item=>{
    var val=yearCount[item.id.year];
  if (val){
        yearCount[item.id.year]=val+item.Count;
  }
  else{
    yearCount[item.id.year]=item.Count;
  }
})
console.log(yearCount);