Skip to main content
improved formatting
Source Link
adiga
  • 35.4k
  • 9
  • 65
  • 88
const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))
const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))
const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))
const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))
improved formatting
Source Link
adiga
  • 35.4k
  • 9
  • 65
  • 88

You could use reduce to group them in to an accumulator object with "0-1s" and "1-2s" as keys. Then use Object.values()Object.values() to get the array of values:

const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))
const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))

If each object has more dynamic time ranges like 0-1s etc, you could destrcuture the object in the reduce parameter to get the country and looprest of the time ranges to separate properties. Loop through the entries of rest to update value for each objecttime range:

You could use reduce to group them in to an accumulator object with "0-1s" and "1-2s" as keys. Then use Object.values() to get the array of values:

const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))

If each object has more dynamic time ranges like 0-1s etc, you could destrcuture the object in the reduce parameter and loop through entries of each object:

You could use reduce to group them in to an accumulator object with "0-1s" and "1-2s" as keys. Then use Object.values() to get the array of values:

const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))

If each object has more dynamic time ranges like 0-1s etc, you could destrcuture the object in the reduce parameter to get the country and rest of the time ranges to separate properties. Loop through the entries of rest to update value for each time range:

improved formatting
Source Link
adiga
  • 35.4k
  • 9
  • 65
  • 88

You could use reduce to group them in to an accumulator object with "0-1s" and "1-2s" as keys. Then use Object.values() to get the array of values:

const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))

If each object has more dynamic time ranges like 0-1s etc, you could destrcuture the object in the reduce parameter and loop through entries of each object:

const input = [{"0-1s":6,"1-2s":2,country:"us"},{"0-1s":1,"1-2s":4,country:"ja"},{"0-1s":3,"1-2s":9,country:"ca"}];  const grouped = input.reduce((r, { country, ...rest }) => { Object.entries(rest).forEach(([time, v]) => { r[time] = r[time] || { time }; r[time][country] = v })  return r; }, {}) console.log(Object.values(grouped))

You could use reduce to group them in to an accumulator object with "0-1s" and "1-2s" as keys. Then use Object.values() to get the array of values:

const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || {}; r["1-2s"] = r["1-2s"] || {}; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))

You could use reduce to group them in to an accumulator object with "0-1s" and "1-2s" as keys. Then use Object.values() to get the array of values:

const input = [{ "0-1s": 6, "1-2s": 2, country: "us" }, { "0-1s": 1, "1-2s": 4, country: "ja" }, { "0-1s": 3, "1-2s": 9, country: "ca" }] const grouped = input.reduce((r, o) => { r["0-1s"] = r["0-1s"] || { time: "0-1s" }; r["1-2s"] = r["1-2s"] || { time: "1-2s" }; r["0-1s"][o.country] = o["0-1s"] r["1-2s"][o.country] = o["1-2s"] return r; }, {}) console.log(Object.values(grouped))

If each object has more dynamic time ranges like 0-1s etc, you could destrcuture the object in the reduce parameter and loop through entries of each object:

const input = [{"0-1s":6,"1-2s":2,country:"us"},{"0-1s":1,"1-2s":4,country:"ja"},{"0-1s":3,"1-2s":9,country:"ca"}];  const grouped = input.reduce((r, { country, ...rest }) => { Object.entries(rest).forEach(([time, v]) => { r[time] = r[time] || { time }; r[time][country] = v })  return r; }, {}) console.log(Object.values(grouped))

Source Link
adiga
  • 35.4k
  • 9
  • 65
  • 88
Loading