将对象数组与内部字符串数组 javascript 分组
group array of objects with inner array of strings javascript
我有一个对象数组,如下所示 -
const books = [
{
name:"abc",
isbn: 123,
tags: ["tagA","tagB","tagC"]
},
{
name:"xyz",
isbn: 456,
tags: ["tagB","tagC"]
},
{
name:"pqr",
isbn: 456,
tags: ["tagB"]
}
];
我想根据每个对象的 tags
对其进行分组,并将匹配的对象推送到字符串数组的 tags
值中。我的预期输出应该是一个以分组值作为键的对象,该值应该是匹配值的数组。
我的预期输出是-
const expected = {
"tagA" : [
{
name:"abc",
isbn: 123,
},
],
"tagB" : [
{
name:"abc",
isbn: 123,
},
{
name:"xyz",
isbn: 456,
},
{
name:"pqr",
isbn: 456,
},
],
"tagC" : [
{
name:"abc",
isbn: 123,
},
{
name:"xyz",
isbn: 456,
},
],
}
键值分组也有类似问题;因为你想根据数组的键值进行分组,你可以这样做:
const books = [
{
name:"abc",
isbn: 123,
tags: ["tagA","tagB","tagC"]
},
{
name:"xyz",
isbn: 456,
tags: ["tagB","tagC"]
},
{
name:"pqr",
isbn: 456,
tags: ["tagB"]
}
];
let grouped = {};
books.forEach((book)=>{
book.tags.forEach(tag=>{
if(!grouped[tag]) grouped[tag] = [{name:book.name,isbn:book.isbn}]
else grouped[tag].push({name:book.name,isbn:book.isbn})
})
})
console.log(grouped)
这是一个非常标准的 'group by',带有一个额外的循环以将每个元素添加到多个组中。
此处使用destructuring to isolate the tags
array from the rest of the book
object, iterating over each tag creating a property in the result using logical nullish assignment (??=) if it doesn't already exist, and then pushing a clone of each book
using spread syntax
const books = [{ name: "abc", isbn: 123, tags: ["tagA", "tagB", "tagC"] }, { name: "xyz", isbn: 456, tags: ["tagB", "tagC"] }, { name: "pqr", isbn: 456, tags: ["tagB"] }];
const result = {};
for (const { tags, ...book } of books) {
for (const tag of tags) {
(result[tag] ??= []).push({...book});
}
}
console.log(result);
我有一个对象数组,如下所示 -
const books = [
{
name:"abc",
isbn: 123,
tags: ["tagA","tagB","tagC"]
},
{
name:"xyz",
isbn: 456,
tags: ["tagB","tagC"]
},
{
name:"pqr",
isbn: 456,
tags: ["tagB"]
}
];
我想根据每个对象的 tags
对其进行分组,并将匹配的对象推送到字符串数组的 tags
值中。我的预期输出应该是一个以分组值作为键的对象,该值应该是匹配值的数组。
我的预期输出是-
const expected = {
"tagA" : [
{
name:"abc",
isbn: 123,
},
],
"tagB" : [
{
name:"abc",
isbn: 123,
},
{
name:"xyz",
isbn: 456,
},
{
name:"pqr",
isbn: 456,
},
],
"tagC" : [
{
name:"abc",
isbn: 123,
},
{
name:"xyz",
isbn: 456,
},
],
}
键值分组也有类似问题;因为你想根据数组的键值进行分组,你可以这样做:
const books = [
{
name:"abc",
isbn: 123,
tags: ["tagA","tagB","tagC"]
},
{
name:"xyz",
isbn: 456,
tags: ["tagB","tagC"]
},
{
name:"pqr",
isbn: 456,
tags: ["tagB"]
}
];
let grouped = {};
books.forEach((book)=>{
book.tags.forEach(tag=>{
if(!grouped[tag]) grouped[tag] = [{name:book.name,isbn:book.isbn}]
else grouped[tag].push({name:book.name,isbn:book.isbn})
})
})
console.log(grouped)
这是一个非常标准的 'group by',带有一个额外的循环以将每个元素添加到多个组中。
此处使用destructuring to isolate the tags
array from the rest of the book
object, iterating over each tag creating a property in the result using logical nullish assignment (??=) if it doesn't already exist, and then pushing a clone of each book
using spread syntax
const books = [{ name: "abc", isbn: 123, tags: ["tagA", "tagB", "tagC"] }, { name: "xyz", isbn: 456, tags: ["tagB", "tagC"] }, { name: "pqr", isbn: 456, tags: ["tagB"] }];
const result = {};
for (const { tags, ...book } of books) {
for (const tag of tags) {
(result[tag] ??= []).push({...book});
}
}
console.log(result);