简体   繁体   English

如果数组的长度大于 1,则 JSON 将 object 分离到新对象

[英]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'] },
etc ETC

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.entriesmap

 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.

相关问题 Array.length比数组长 - Array.length longer than array json 对象数组的长度 - Length of array of json objects 如何将对象数组放入新的Json对象 - How to put array of objects into new Json object 将具有不同长度的深层嵌套JSON对象重新格式化为简化对象数组 - Reformatting deeply nested JSON object with varying length into an array of simplified objects 使用Javascript读取多个对象Json数组中对象的长度 - Reading length of on of the object in a Multiple objects Json Array using Javascript 如何 map 对象数组并返回每个对象值的长度? - How to map Array of Objects and return the length of each Object's value? 如何在对象数组中查找对象,谁的嵌套数组匹配单独数组中的所有对象? - How to find object in array of objects, who's nested array matches all the objects in a separate array? 创建一个具有键值数组的空状态对象,该键值数组以JSON对象作为值,然后更改这些json对象 - creating an empty state object that has a key value array with JSON objects as the values and than changing those json objects 根据对象的日期年份将对象数组拆分为新数组 - Split array of objects into new arrays based on year of object's date 如何将带有数组的JSON对象拆分为单独的对象 - How to split JSON object with arrays into separate objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM