像这样的对象中有多个字段。
{
"image-processing": {
"type": "lib",
"data": {
"files": [
{
"file": "libs/photography/processing/.babelrc",
"hash": "62d50f586b2880f9d58ca4e9a84914c6a0d4936d"
},
{
"file": "libs/photography/processing/src/lib/processing.ts",
"hash": "a285d3554f264cc60f35bef6180298badb0478d1",
"deps": [
"npm:gm",
"npm:mongodb"
]
}
]
}
},
"image-processing-2": {
"type": "lib",
"data": {
"files": [
{
"file": "libs/photography/processing/.babelrc",
"hash": "62d50f586b2880f9d58ca4e9a84914c6a0d4936d",
"deps": [
"npm:gm",
"npm:faker"
]
},
{
"file": "libs/photography/processing/src/lib/processing.ts",
"hash": "a285d3554f264cc60f35bef6180298badb0478d1",
"deps": [
"npm:gm",
"npm:mongodb"
]
}
]
}
}
}我只需要将data.files.deps的类型和内容作为键的值对象。如您所见,每个文件都有一个可选的deps数组。所有的deps值都应该合并。
因此,结果应该是:
{
"image-processing": { type: "lib", packages: [ "npm:gm", "npm:mongodb" ] },
"image-processing-2": { type: "lib", packages: [ "npm:faker", "npm:gm", "npm:mongodb" ] }
}我试图遍历该对象,但这将覆盖该值,而不是向该值添加新的包名:
const result = {}
Object.entries(data).map(([key, value]) => {
result[key].type = value.type
result[key].packages = value?.data?.files.map(d => d.deps)
})发布于 2022-04-24 21:18:29
使用flatMap而不是map,这样就可以有一个扁平的依赖字符串数组,并与空数组交替使用,这样未定义的.deps属性就不会造成问题。
const data={"image-processing":{type:"lib",data:{files:[{file:"libs/photography/processing/.babelrc",hash:"62d50f586b2880f9d58ca4e9a84914c6a0d4936d"},{file:"libs/photography/processing/src/lib/processing.ts",hash:"a285d3554f264cc60f35bef6180298badb0478d1",deps:["npm:gm","npm:mongodb"]}]}},"image-processing-2":{type:"lib",data:{files:[{file:"libs/photography/processing/.babelrc",hash:"62d50f586b2880f9d58ca4e9a84914c6a0d4936d",deps:["npm:gm","npm:faker"]},{file:"libs/photography/processing/src/lib/processing.ts",hash:"a285d3554f264cc60f35bef6180298badb0478d1",deps:["npm:gm","npm:mongodb"]}]}}};
const output = Object.fromEntries(
Object.entries(data).map(([key, value]) => [
key,
{
type: 'lib',
packages: [...new Set(value.data.files.flatMap(file => file.deps ?? []))]
}
])
);
console.log(output);
发布于 2022-04-24 22:09:11
根据您的新评论,只需将逻辑分开一点:
迭代在Object.entries上获得一个键/值对(值是对象),然后遍历每个对象的文件。如果有任何filter出"npm“,那么map在该数组上并创建一个具有更新名称的新数组。
将该数组添加到packages数组中。
最后,压扁包数组,通过从它创建一套新的来简化它,然后将把它传开作为数组中的值,然后使用键添加到输出对象中。
const data={"image-processing":{type:"lib",data:{files:[{file:"libs/photography/processing/.babelrc",hash:"62d50f586b2880f9d58ca4e9a84914c6a0d4936d"},{file:"libs/photography/processing/src/lib/processing.ts",hash:"a285d3554f264cc60f35bef6180298badb0478d1",deps:["npm:gm","npm:mongodb"]}]}},"image-processing-2":{type:"lib",data:{files:[{file:"libs/photography/processing/.babelrc",hash:"62d50f586b2880f9d58ca4e9a84914c6a0d4936d",deps:["npm:gm","npm:faker"]},{file:"libs/photography/processing/src/lib/processing.ts",hash:"a285d3554f264cc60f35bef6180298badb0478d1",deps:["npm:gm","npm:mongodb"]}]}}};
// Output object
const out = {};
// Iterate over the object entries
for (const [key, obj] of Object.entries(data)) {
// Destructure the type, and the files
// array from each object
const { type, data: { files } } = obj;
const packages = [];
// Iterate over each `files` array
for (const file of files) {
if (file.deps) {
// `filter` out the "npm" names, and
// then `map` over that array to update the names
const list = file.deps
.filter(dep => dep.startsWith('npm:'))
.map(dep => dep.replace('npm:', ''));
// Bang that completed array into `packages`
packages.push(list);
}
}
// Finally create a new key/value on the output
// object by setting the value as an object
// with `type`, and the flattened, deduped packages array
out[key] = {
type,
packages: [...new Set(packages.flat())]
};
}
console.log(out);
https://stackoverflow.com/questions/71992384
复制相似问题