[英]Iterate and group the objects using map function
Check for the decimal id and group them accordingly.检查十进制 ID 并相应地对它们进行分组。
Below are the sample and recommended JSON's以下是示例和推荐的 JSON
Sample JSON示例 JSON
{
"results": [
{
"name": "Download",
"id": "1.1.1"
},
{
"name": "Download",
"id": "1.2"
},
{
"name": "Download",
"id": "1.3.2"
},
{
"name": "Download",
"id": "2"
},
{
"name": "Download",
"id": "2.3"
},
{
"name": "Download",
"id": "3.2"
},
{
"name": "Download",
"id": "3.5"
},
{
"name": "Download",
"id": "4.2"
}
]
}
Would like to iterate and Re-structure the above JSON into below recommended format.想将上述 JSON 迭代和重组为以下推荐格式。
Logic: Should check the id(with and without decimals) and group them based on the number.逻辑:应该检查id(带小数点和不带小数)并根据数字对它们进行分组。
For Example:例如:
1, 1.1, 1.2.3, 1.4.5 => data1: [{id: 1},{id: 1.1}....]
2, 2.3, 2.3.4 => data2: [{id: 2},{id: 2.3}....]
3, 3.1 => data3: [{id: 3},{id: 3.1}]
Recommended JSON推荐的 JSON
{
"results": [
{
"data1": [
{
"name": "Download",
"id": "1.1.1"
},
{
"name": "Download",
"id": "1.2"
},
{
"name": "Download",
"id": "1.3.2"
}
]
},
{
"data2": [
{
"name": "Download",
"id": "2"
},
{
"name": "Download",
"id": "2.3"
}
]
},
{
"data3": [
{
"name": "Download",
"id": "3.2"
},
{
"name": "Download",
"id": "3.5"
}
]
},
{
"data4": [
{
"name": "Download",
"id": "4.2"
}
]
}
]
}
I have tried the below solution but it doesn't group the object我已经尝试了以下解决方案,但它没有将对象分组
var formatedJSON = [];
results.map(function(d,i) {
formatedJSON.push({
[data+i]: d
})
});
Thanks in advance.提前致谢。
You can use reduce
like this.你可以像这样使用reduce
。 The idea is to create a key-value pair for each data1
, data2
etc so that values in this object are the values you need in the final array.这个想法是为每个data1
、 data2
等创建一个键值对,这样这个对象中的值就是你在最终数组中需要的值。 Then use Object.values
to get those as an array.然后使用Object.values
将它们作为数组获取。
const sampleJson = {"results":[{"name":"Download","id":"1.1.1"},{"name":"Download","id":"1.2"},{"name":"Download","id":"1.3.2"},{"name":"Download","id":"2"},{"name":"Download","id":"2.3"},{"name":"Download","id":"3.2"},{"name":"Download","id":"3.5"},{"name":"Download","id":"4.2"}]} const grouped = sampleJson.results.reduce((a, v) => { const key = `data${parseInt(v.id)}`; (a[key] = a[key] || {[key]: []})[key].push(v); return a; },{}); console.log({results: Object.values(grouped)})
One liner / Code-golf:一个班轮/代码高尔夫:
let s={"results":[{"name":"Download","id":"1.1.1"},{"name":"Download","id":"1.2"},{"name":"Download","id":"1.3.2"},{"name":"Download","id":"2"},{"name":"Download","id":"2.3"},{"name":"Download","id":"3.2"},{"name":"Download","id":"3.5"},{"name":"Download","id":"4.2"}]},k; console.log({results:Object.values(s.results.reduce((a,v)=>(k=`data${parseInt(v.id)}`,(a[k] = a[k]||{[k]:[]})[k].push(v),a),{}))})
Here you go:干得好:
var data = { "results": [ { "name": "Download", "id": "1.1.1" }, { "name": "Download", "id": "1.2" }, { "name": "Download", "id": "1.3.2" }, { "name": "Download", "id": "2" }, { "name": "Download", "id": "2.3" }, { "name": "Download", "id": "3.2" }, { "name": "Download", "id": "3.5" }, { "name": "Download", "id": "4.2" } ] }; let newSet = new Set(); data.results.forEach(e => { let key = e.id.substring(0, e.id.indexOf('.')); console.log(key); if (newSet.has(key) == false) { newSet.add(key); newSet[key] = []; } newSet[key].push(e.id); }); console.log(newSet);
Here's how you'd do it:这是你的方法:
var data = { "results": [ { "name": "Download", "id": "1.1.1" }, { "name": "Download", "id": "1.2" }, { "name": "Download", "id": "1.3.2" }, { "name": "Download", "id": "2" }, { "name": "Download", "id": "2.3" }, { "name": "Download", "id": "3.2" }, { "name": "Download", "id": "3.5" }, { "name": "Download", "id": "4.2" } ] }; var newData = { "results": {} }; data.results.forEach(item => { var num = item.id.slice(0, 1); if (newData.results["data" + num]) { newData.results["data" + num].push(item); } else { newData.results["data" + num] = [item]; } }) data = newData; console.log(data);
What this does is it iterates through each item in results
, gets the number at the front of this item's id
, and checks if an array of the name data-{num}
exists.它的作用是遍历results
每个项目,获取该项目id
前面的数字,并检查名称为data-{num}
的数组是否存在。 If the array exists, it's pushed.如果数组存在,则将其推送。 If it doesn't exist, it's created with the item.如果它不存在,则使用该项目创建它。
let input = getInput(); let output = input.reduce((acc, curr)=>{ let {id} = curr; let majorVersion = 'name' + id.split('.')[0]; if(!acc[majorVersion]) acc[majorVersion]= []; acc[majorVersion].push(curr); return acc; },{}) console.log(output) function getInput(){ return [ { "name": "Download", "id": "1.1.1" }, { "name": "Download", "id": "1.2" }, { "name": "Download", "id": "1.3.2" }, { "name": "Download", "id": "2" }, { "name": "Download", "id": "2.3" }, { "name": "Download", "id": "3.2" }, { "name": "Download", "id": "3.5" }, { "name": "Download", "id": "4.2" } ] }
One solution with RegEx for finer control as it would differentiate easily between 1 and 11. Also this will make sure that even if the same version comes in end(say 1.9 in end) it will put it back in data1.使用 RegEx 进行更精细控制的一种解决方案,因为它可以轻松区分 1 和 11。此外,这将确保即使最终版本相同(例如 1.9),它也会将其放回 data1。
let newArr2 = ({ results }) =>
results.reduce((acc, item) => {
let key = "data" + /^(\d+)\.?.*/.exec(item.id)[1];
let found = acc.find(i => key in i);
found ? found[key].push(item) : acc.push({ [key]: [item] });
return acc;
}, []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.