[英]JSON separate object to new objects if array's length is longer than 1
Edit: Answers don't work if there's more objects inside JSON:/ If you can take a look one more time I would be grateful.编辑:如果 JSON 内有更多对象,答案将不起作用:/ 如果您能再看一次,我将不胜感激。
I am trying to seperate JSON array to make new objects with single arrays.我正在尝试分离 JSON 数组以使用单个 arrays 制作新对象。
const objs = [{ id: ["18772729849", "17773133341", "17773173696"], o: 552750, g: 2, p: 5427.37, f: ["0.07480893", "0.07703563", "0.07160716"], b: ["76561198311594977", "76561198311424548", "76561198312084911"], d: ["uYEY1BQ", "jSdgIK5", "lldBCJt"], }, { id: ["5122519", "7125125", "124124"], o: 12440, g: 412, p: 4124, f: ["0.033", "0.0123", "0.412412"], b: ["76561198124421421", "712412441248", "71241244911"], d: ["uYasdasd1BQ", "jSasdasdK5", "llfasCJt"], } ];
And my output should look like that:我的 output 应该是这样的:
[{ id: ['18772729849'], o: 552750, g: 2, p: 5427.37, f: ['0.07480893'], b: ['76561198311594977'], d: ['uYEY1BQ'] }, { id: ['17773133341'], o: 552750, g: 2, p: 5427.37, f: ['0.07703563'], b: ['76561198311424548'], d: ['jSdgIK5'] }, { id: ['17773173696'], o: 552750, g: 2, p: 5427.37, f: ['0.07160716'], b: ['76561198312084911'], d: ['lldBCJt5'] }, { id: ['5122519'], o: 12440, g: 412, p: 4124, f: ['0.033'], b: ['76561198124421421'], d: ['uYasdasd1BQ'] }, { id: ['7125125'], o: 12440, g: 412, p: 4124, f: ['0.0123'], b: ['712412441248'], d: ['jSasdasdK5'] },
So "o", "g", "p" stays the same as in original object.所以“o”、“g”、“p”与原来的 object 保持一致。
Solution:解决方案:
const objs = [{ id: ["18772729849", "17773133341", "17773173696"], o: 552750, g: 2, p: 5427.37, f: ["0.07480893", "0.07703563", "0.07160716"], b: ["76561198311594977", "76561198311424548", "76561198312084911"], d: ["uYEY1BQ", "jSdgIK5", "lldBCJt"],},{ id: ["5122519", "7125125", "124124"], o: 12440, g: 412, p: 4124, f: ["0.033", "0.0123", "0.412412"], b: ["76561198124421421", "712412441248", "71241244911"], d: ["uYasdasd1BQ", "jSasdasdK5", "llfasCJt"]}]; var result = objs.flatMap(obj=>{ return Array.from({length:obj.id.length},(_,i)=>Object.fromEntries( Object.entries(obj).map(([key, val])=>[key, Array.isArray(val)? [val[i]]: val]) )); }); console.log(result);
Use Object.entries
and map
使用Object.entries
和map
const process = (arr) => { const output = []; arr.forEach((obj) => { obj.id.forEach((_, i) => { output.push( Object.fromEntries( Object.entries(obj).map(([key, value]) => [ key, Array.isArray(value)? [value[i]]: value, ]) ) ); }); }); return output; }; const objs = [ { id: ["18772729849", "17773133341", "17773173696"], o: 552750, g: 2, p: 5427.37, f: ["0.07480893", "0.07703563", "0.07160716"], b: ["76561198311594977", "76561198311424548", "76561198312084911"], d: ["uYEY1BQ", "jSdgIK5", "lldBCJt"], }, { id: ["5122519", "7125125", "124124"], o: 12440, g: 412, p: 4124, f: ["0.033", "0.0123", "0.412412"], b: ["76561198124421421", "712412441248", "71241244911"], d: ["uYasdasd1BQ", "jSasdasdK5", "llfasCJt"], }, ]; console.log(process(objs));
Actually just go with a .map()
实际上只是带有.map()
的 go
let data = { id: [ '18772729849', '17773133341', '17773173696' ], o: 552750, g: 2, p: 5427.37, f: [ '0.07480893', '0.07703563', '0.07160716' ], b: [ '76561198311594977', '76561198311424548', '76561198312084911' ], d: [ 'uYEY1BQ', 'jSdgIK5', 'lldBCJt' ] } let result = data.id.map((el,i) => { return { id: [el], o: data.o, g: data.g, p: data.p, f: [data.f[i]], b: [data.b[i]], d: [data.d[i]] } }) console.log(result);
You can take fromEntries
of key value pair by mapping it and to generate new array you may use Array.from
based on the length of the id
attribute.您可以通过映射来获取键值对的fromEntries
并生成新数组,您可以使用Array.from
基于id
属性的长度。 Here is my try:这是我的尝试:
const objs = [{ id: ["18772729849", "17773133341", "17773173696"], o: 552750, g: 2, p: 5427.37, f: ["0.07480893", "0.07703563", "0.07160716"], b: ["76561198311594977", "76561198311424548", "76561198312084911"], d: ["uYEY1BQ", "jSdgIK5", "lldBCJt"],},{ id: ["5122519", "7125125", "124124"], o: 12440, g: 412, p: 4124, f: ["0.033", "0.0123", "0.412412"], b: ["76561198124421421", "712412441248", "71241244911"], d: ["uYasdasd1BQ", "jSasdasdK5", "llfasCJt"]}]; var result = objs.flatMap(obj=>{ return Array.from({length:obj.id.length},(_,i)=>Object.fromEntries( Object.entries(obj).map(([key, val])=>[key, Array.isArray(val)? [val[i]]: val]) )); }); console.log(result);
You can find out the array with maximum length and then based on that generate your array of object.您可以找出最大长度的数组,然后在此基础上生成您的 object 数组。
const objs = [{ id: ["18772729849", "17773133341", "17773173696"], o: 552750, g: 2, p: 5427.37, f: ["0.07480893", "0.07703563", "0.07160716"], b: ["76561198311594977", "76561198311424548", "76561198312084911"], d: ["uYEY1BQ", "jSdgIK5", "lldBCJt"], }, { id: ["5122519", "7125125", "124124"], o: 12440, g: 412, p: 4124, f: ["0.033", "0.0123", "0.412412"], b: ["76561198124421421", "712412441248", "71241244911"], d: ["uYasdasd1BQ", "jSasdasdK5", "llfasCJt"], } ], output = objs.map(o => { const length = Object.keys(o).reduce((r, k) => { if (Array.isArray(o[k])) { r = o[k].length > r? o[k].length: r; } return r; }, 0); return Array.from({ length }, (_, i) => { return Object.keys(o).reduce((r, k) => { r[k] = Array.isArray(o[k])? [o[k][i] || '']: o[k]; return r; }, {}); }); }).flat(); console.log(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.