Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

javascript - 如何转换要按对象键分组的对象(How can I transform an object to be grouped by object keys)

I have this object which also contains arrays and objects and which is currently grouped by "category"(我有这个对象,它也包含数组和对象,当前按“类别”分组)

data = { vehicles: [ { name: "Team1", cars: [ { multiple: 0.5, category: "cars" }, { multiple: 0.5, category: "cars" } ], bike: [ { multiple: 0.5, category: "bike" } ], scooter: [ { multiple: 0.5, category: "scooter" } ] }, { name: "Team2", cars: [ { multiple: 0.5, category: "cars" }, { multiple: 0.5, category: "cars" } ], bike: [ { multiple: 0.5, category: "bike" } ], scooter: [ { multiple: 0.5, category: "scooter" } ] } ] }; I used this line of code to groupby category but(我使用这行代码对类别进行分组,但是) return xs.reduce(function(rv, x) { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {}); }; var data = groupBy(data, "category"); Since I have the object "data" now I want to perfrom further transformation My question is it possible to groupby two categories so the output looks like this(由于我现在有了对象“ data”,因此我想进行进一步的转换。我的问题是有可能将两个类别分组,以便输出看起来像这样) data = { vehicles: [ { name: "Team1", cars: [ { multiple: 0.5, category: "cars" }, { multiple: 0.5, category: "cars" } ], bike_scooter: [ { multiple: 0.5, category: "bike" }, { multiple: 0.5, category: "scooter" } ] }, { name: "Team2", cars: [ { multiple: 0.5, category: "cars" }, { multiple: 0.5, category: "cars" } ], bike_scooter: [ { multiple: 0.5, category: "bike" }, { multiple: 0.5, category: "scooter" } ] } ] };   ask by newbiexc translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could do this with two reduce methods one to build vehicles array and other one to group in each object by provided array of keys.(您可以使用two reduce方法来实现此目的,一种是构建车辆数组,另一种是通过提供的键数组将每个对象分组。)

const data = {"vehicles":[{"name":"Team1","cars":[{"multiple":0.5,"category":"cars"},{"multiple":0.5,"category":"cars"}],"bike":[{"multiple":0.5,"category":"bike"}],"scooter":[{"multiple":0.5,"category":"scooter"}]},{"name":"Team2","cars":[{"multiple":0.5,"category":"cars"},{"multiple":0.5,"category":"cars"}],"bike":[{"multiple":0.5,"category":"bike"}],"scooter":[{"multiple":0.5,"category":"scooter"}]}]}; function groupBy(data, keys) { return data.vehicles.reduce((r, {name,...rest}) => { const obj = Object.entries(rest).reduce((a, [k, v]) => { if (keys.includes(k)) { const key = keys.join('_'); if (!a[key]) a[key] = [...v]; else a[key].push(...v); } else a[k] = [...v] return a }, {name}) r.vehicles.push(obj) return r; }, { vehicles: [] }) } console.log(groupBy(data, ['bike', 'scooter'])) console.log(groupBy(data, ['bike', 'cars']))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...