如何将对象数组映射到某个字段聚集/讨人喜欢的不同数组?

发布于 2025-02-13 07:17:13 字数 721 浏览 1 评论 0原文

我已经有一系列的对象:

[
    {
        "n": "David",
        "t": 1,
        "o": "2"
    },
    {
        "n": "Paul",
        "t": 3,
        "o": "4"
    },
    {
        "n": "David",
        "t": 5,
        "o": "6"
    },
    {
        "n": "David",
        "t": 7,
        "o": "8"
    },
    {
        "n": "Paul",
        "t": 9,
        "o": "10"
    }
]

我需要映射到以下内容:

[
    [
        "David",
        [
            [1, "2"],
            [5, "6"],
            [7, "8"]
        ]
    ],
    [
        "Paul",
        [
            [3, "4"],
            [9, "10"]
        ]
    ]
]

因此,基本上是一个数组,其中每个内部数组是“ the field ” n“ n”字段的“组”,所有元素汇总了成为阵列。

I've this array of objects:

[
    {
        "n": "David",
        "t": 1,
        "o": "2"
    },
    {
        "n": "Paul",
        "t": 3,
        "o": "4"
    },
    {
        "n": "David",
        "t": 5,
        "o": "6"
    },
    {
        "n": "David",
        "t": 7,
        "o": "8"
    },
    {
        "n": "Paul",
        "t": 9,
        "o": "10"
    }
]

And I need to map to this:

[
    [
        "David",
        [
            [1, "2"],
            [5, "6"],
            [7, "8"]
        ]
    ],
    [
        "Paul",
        [
            [3, "4"],
            [9, "10"]
        ]
    ]
]

So basically an array of array, where each inner array is a "group by" the field "n" field and all elements aggregated become arrays.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小忆控 2025-02-20 07:17:13

使用降低的通常解决方案。组成n,并使用object.values进行对象的值

const a = [    {        "n": "David",        "t": 1,        "o": "2"    },    {        "n": "Paul",        "t": 3,        "o": "4"    },    {        "n": "David",        "t": 5,"o": "6"    },    {        "n": "David",        "t": 7,        "o": "8"    },{        "n": "Paul",        "t": 9,        "o": "10"    }]

const res = Object.values(a.reduce((acc,{n,t,o}) => {
  acc[n]??=[n,[]]
  acc[n][1].push([t,o])
  return acc
},{}))
console.log(res)

或使用循环的

const a = [    {        "n": "David",        "t": 1,        "o": "2"    },    {        "n": "Paul",        "t": 3,        "o": "4"    },    {        "n": "David",        "t": 5,"o": "6"    },    {        "n": "David",        "t": 7,        "o": "8"    },{        "n": "Paul",        "t": 9,        "o": "10"    }]

const res = {}
for(el of a){
  const {n,o,t} = el
  res[n] = res[n] || [n,[]]
  res[n][1].push([t,o])
}

console.log(Object.values(res))

The usual solution using reduce. Group by the n and the take the values of object using Object.values

const a = [    {        "n": "David",        "t": 1,        "o": "2"    },    {        "n": "Paul",        "t": 3,        "o": "4"    },    {        "n": "David",        "t": 5,"o": "6"    },    {        "n": "David",        "t": 7,        "o": "8"    },{        "n": "Paul",        "t": 9,        "o": "10"    }]

const res = Object.values(a.reduce((acc,{n,t,o}) => {
  acc[n]??=[n,[]]
  acc[n][1].push([t,o])
  return acc
},{}))
console.log(res)

or using a for of loop

const a = [    {        "n": "David",        "t": 1,        "o": "2"    },    {        "n": "Paul",        "t": 3,        "o": "4"    },    {        "n": "David",        "t": 5,"o": "6"    },    {        "n": "David",        "t": 7,        "o": "8"    },{        "n": "Paul",        "t": 9,        "o": "10"    }]

const res = {}
for(el of a){
  const {n,o,t} = el
  res[n] = res[n] || [n,[]]
  res[n][1].push([t,o])
}

console.log(Object.values(res))

聽兲甴掵 2025-02-20 07:17:13

要获取输出,请使用对象将存储的映射数组的索引存储在名称中。 [例如,大卫是0,对于保罗来说,是1]。
我们可以在数组中使用字符串索引来可读目的,但我认为这不是一个好主意,因为输出模式并不那么复杂。

const data = [
    {
        "n": "David",
        "t": 1,
        "o": "2"
    },
    {
        "n": "Paul",
        "t": 3,
        "o": "4"
    },
    {
        "n": "David",
        "t": 5,
        "o": "6"
    },
    {
        "n": "David",
        "t": 7,
        "o": "8"
    },
    {
        "n": "Paul",
        "t": 9,
        "o": "10"
    }
];

// to save the index for the name
const nameHash = {};
// to save the output
const output = [];

for (let obj of data) {
  const {n, t, o} = obj;

  if (!(n in nameHash)) {
    nameHash[n] = output.length;
    output.push([n, []]);
  }
  let index = nameHash[n];
  
  output[index][1].push([t, o]);
}

console.log(output);

To get the output, use object to store the index of the stored mapped array with name. [e.g. for David, it's 0 and for Paul, it's 1].
We can use string Index in the array for the readable purpose but I don't think that's a great idea as output schema is not that complex.

const data = [
    {
        "n": "David",
        "t": 1,
        "o": "2"
    },
    {
        "n": "Paul",
        "t": 3,
        "o": "4"
    },
    {
        "n": "David",
        "t": 5,
        "o": "6"
    },
    {
        "n": "David",
        "t": 7,
        "o": "8"
    },
    {
        "n": "Paul",
        "t": 9,
        "o": "10"
    }
];

// to save the index for the name
const nameHash = {};
// to save the output
const output = [];

for (let obj of data) {
  const {n, t, o} = obj;

  if (!(n in nameHash)) {
    nameHash[n] = output.length;
    output.push([n, []]);
  }
  let index = nameHash[n];
  
  output[index][1].push([t, o]);
}

console.log(output);

剧终人散尽 2025-02-20 07:17:13

您可以使用array.Reduceobject.entries进行操作。

const transform = data => Object.entries(data.reduce((res, {n,t,o}) => {
  const existing = res[n] || []
  return {
    ...res,
    [n]: [...existing, [t, o]]
  }
}, {}))

const data = [
    {
        "n": "David",
        "t": 1,
        "o": "2"
    },
    {
        "n": "Paul",
        "t": 3,
        "o": "4"
    },
    {
        "n": "David",
        "t": 5,
        "o": "6"
    },
    {
        "n": "David",
        "t": 7,
        "o": "8"
    },
    {
        "n": "Paul",
        "t": 9,
        "o": "10"
    }
]

console.log(transform(data))

you can do it using Array.reduce and Object.entries

const transform = data => Object.entries(data.reduce((res, {n,t,o}) => {
  const existing = res[n] || []
  return {
    ...res,
    [n]: [...existing, [t, o]]
  }
}, {}))

const data = [
    {
        "n": "David",
        "t": 1,
        "o": "2"
    },
    {
        "n": "Paul",
        "t": 3,
        "o": "4"
    },
    {
        "n": "David",
        "t": 5,
        "o": "6"
    },
    {
        "n": "David",
        "t": 7,
        "o": "8"
    },
    {
        "n": "Paul",
        "t": 9,
        "o": "10"
    }
]

console.log(transform(data))

五里雾 2025-02-20 07:17:13

从数据中制作一个字典,然后将结果以所需的格式放置。

const data=[{n:"David",t:1,o:"2"},{n:"Paul",t:3,o:"4"},{n:"David",t:5,o:"6"},{n:"David",t:7,o:"8"},{n:"Paul",t:9,o:"10"}];
let dict = {};
data.forEach(d => {
  if (!dict[d.n]){
    dict[d.n] = [[d.t, d.o]];
  }
  else {
    dict[d.n].push([d.t, d.o]);
  }
});
let result = [];
Object.entries(dict).forEach(d => {
  result.push([d[0],d[1]]);
});
console.log(result);

Make a dictionary out of the data and then put the result in the desired format.

const data=[{n:"David",t:1,o:"2"},{n:"Paul",t:3,o:"4"},{n:"David",t:5,o:"6"},{n:"David",t:7,o:"8"},{n:"Paul",t:9,o:"10"}];
let dict = {};
data.forEach(d => {
  if (!dict[d.n]){
    dict[d.n] = [[d.t, d.o]];
  }
  else {
    dict[d.n].push([d.t, d.o]);
  }
});
let result = [];
Object.entries(dict).forEach(d => {
  result.push([d[0],d[1]]);
});
console.log(result);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文