1 回答
TA贡献1821条经验 获得超4个赞
你可以使用lodash filter。
它将允许您挑选出数组中满足条件的所有对象。
import filter from 'lodash/filter'
const array = [
{id:1,country: "India", city:"city1", year:2019, jan:220, feb:300},
{id:2,country: "India", city:"city1", year:2019, jan:777, feb:555},
{id:3,country: "UK", city:"city2", year:2019, jan:1, feb:12},
]
// gives you a list of all objects that match you conditions.
const filteredArray = filter(array, {country: "India", city: "city1"})
// make a new object with whatever math you wanted to do.
const newObject = {id: array.length, country: "India", city: "city1", year: 2019, jan: filteredArray.map(**some calculation**),feb: filteredArray.map(**some calculation**)}
// to ad it back to the original array of objects
array.push(newObject)
希望能给你一个开始的地方。
添加回答
举报