我有以下JSON:
[
{
"computers": {
"available": [
6,
2
],
"held": [],
"upgrades": [],
"installed": [
"PC-1"
]
},
"version": "0.6.55-0ubuntu12~20.04.4",
"name": "accountsservice",
"summary": "query and manipulate user account information"
},
{
"computers": {
"available": [
209,
211,
210
],
"held": [],
"upgrades": [],
"installed": [
"PC-3"
]
},
"version": "0.6.45-1ubuntu1.3",
"name": "accountsservice",
"summary": "query and manipulate user account information"
}
]我想对数据进行检查,以检查name属性是否有任何重复的值。因此,有两个accountservices,但它们有不同的version值。
我如何组合2,使我只有1 accountservices,但2 version在其中使用ReactJS?其他数据与此无关,因此也可以删除或合并,我只想列出每个name的不同name。
预期产出:
[
{
"version": "0.6.55-0ubuntu12~20.04.4, 0.6.45-1ubuntu1.3",
"name": "accountsservice",
"summary": "query and manipulate user account information"
}
]我尝试使用我在网上找到的以下函数,但无法让它正常工作:
const sanitize = (source_array) => {
let output = [];
source_array.forEach( e => {
if (output.filter( e1 => e1.name === e.name ).length === 0) {
let unique_e = source_array.filter(j => j.name === e.name );
let model = {};
if (unique_e.length === 1)
model = e;
else {
model.name = e.name;
model.computers.installed = e.computers.installed;
model.version = [];
for ( const el of unique_e ) {
const { version } = el;
model.version.push({version});
}
}
output.push(model)
}
});
return output
}发布于 2022-02-11 10:14:09
这是精简功能的完美用例。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
您的清洁功能可能看起来像这样:
const sanitize = (source_array) => {
const output = source_array.reduce((acc, { name, version, summary }) => {
// try to find an entry with same name
const entry = acc.find(e => e.name === name)
if (entry) {
// There was already an entry with this name !
entry.version += `,${version}`
} else {
acc.push({ name, version, summary })
}
return acc
}, []);
return output
}

发布于 2022-02-11 10:16:33
这样就能完成任务
let source_array = [
{
computers: {
available: [6, 2],
held: [],
upgrades: [],
installed: ["PC-1"]
},
version: "0.6.55-0ubuntu12~20.04.4",
name: "accountsservice",
summary: "query and manipulate user account information"
},
{
computers: {
available: [209, 211, 210],
held: [],
upgrades: [],
installed: ["PC-3"]
},
version: "0.6.45-1ubuntu1.3",
name: "accountsservice",
summary: "query and manipulate user account information"
}
];
let output = [];
source_array.map((item) => {
let filter = output.filter((i) => i.name === item.name);
if (filter.length) {
filter[0].version += `, ${item.version}`;
} else {
output.push({
version: item.version,
name: item.name,
summary: item.summary
});
}
});
console.log(output);
发布于 2022-02-11 10:16:57
这也是可行的。此解决方案使用name进行分组,并列出每个name的versions,并按不同版本对计算机进行分组。
const input = [ { computers: { available: [6, 2], held: [], upgrades: [], installed: ["PC-1"], }, version: "0.6.55-0ubuntu12~20.04.4", name: "accountsservice", summary: "query and manipulate user account information", }, { computers: { available: [209, 211, 210], held: [], upgrades: [], installed: ["PC-3"], }, version: "0.6.45-1ubuntu1.3", name: "accountsservice", summary: "query and manipulate user account information", }, ];
const output = Object.entries(
input.reduce((prev, { name, version, computers }) => {
// check for the same name
if (prev[name]) {
prev[name].version.push(version);
// check for the same version of computers in under same name
if (prev[name].computers[version]) {
prev[name].computers[version].push(computers);
} else {
prev[name].computers[version] = [computers];
}
} else {
// name is identified for the first time
// create a new entry for that
prev[name] = {
version: [version],
computers: { [version]: [computers] },
};
}
return prev;
}, {})
).map(([key, { version, computers }]) => ({
name: key,
versions: version,
computers,
}));
console.log(output);
https://stackoverflow.com/questions/71078220
复制相似问题