[英]JS - How do you push same object value in to an array
I've tried a couple hours to find a way to handle JSON like this: 我已经尝试了几个小时来找到一种处理JSON的方法,如下所示:
[
{
"value": "Osteonecrosis",
"Diagnosis_Code": "DIAG002",
"NamaCategory": "Primary Category",
"FK_Diagnosis_Content_ID": 2
},
{
"value": "Malunion",
"Diagnosis_Code": "DIAG002",
"NamaCategory": "Healing",
"FK_Diagnosis_Content_ID": 19
},
{
"value": "Osteonecrosis",
"Diagnosis_Code": "DIAG004",
"NamaCategory": "Primary Category",
"FK_Diagnosis_Content_ID": 2
},
{
"value": "Malunion",
"Diagnosis_Code": "DIAG004",
"NamaCategory": "Healing",
"FK_Diagnosis_Content_ID": 19
}
]
I want to add an array Below NameCategory
just in case the NameCategory
value is duplicate, so the expected result would be: 我想在
NameCategory
下面添加一个数组,以防NameCategory
值重复,所以预期的结果是:
[
{
"NamaCategory": "Primary Category",
"value":[
{
"value": "Osteonecrosis",
"Diagnosis_Code": "DIAG002",
"FK_Diagnosis_Content_ID": 2
},
{
"value": "Osteonecrosis",
"Diagnosis_Code": "DIAG004",
"FK_Diagnosis_Content_ID": 2
}
]
},
{
"NamaCategory": "Healing",
"value":[
{
"value": "Malunion",
"Diagnosis_Code": "DIAG002",
"FK_Diagnosis_Content_ID": 19
},
{
"value": "Malunion",
"Diagnosis_Code": "DIAG004",
"FK_Diagnosis_Content_ID": 19
}
]
}
]
I'm not familiar to handling JSON, so I need help here, 我对处理JSON并不熟悉,所以我需要帮助,
anyone can help me how to handling those json? 任何人都可以帮助我如何处理这些json?
Use reduce method. 使用reduce方法。 This case return a new array , while creating this new array of objects check if there exist an object whose name is same as the
NamaCategory
of the old array using findIndex
. 这种情况返回一个新数组,而创建这个新的对象数组时,使用
findIndex
检查是否存在一个名称与旧数组的NamaCategory
相同的对象。 findIndex
will return -1
if this NamaCategory
does not exist in the new array. 如果新数组中不存在此
NamaCategory
则findIndex
将返回-1
。 If it does not exist create an object with desired value and push it to the new array. 如果它不存在,请创建具有所需值的对象并将其推送到新数组。 If
NamaCategory
exists then just update the value array 如果存在
NamaCategory
,则只需更新值数组
var orgArray = [{"value":"Osteonecrosis","Diagnosis_Code":"DIAG002","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG002","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19},{"value":"Osteonecrosis","Diagnosis_Code":"DIAG004","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG004","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19}]; var newArray = orgArray.reduce(function(acc, curr) { //finding Index in the array where the NamaCategory matched var findIfNameExist = acc.findIndex(function(item) { return item.NamaCategory === curr.NamaCategory; }) // if in the new array no such object exist where // namecategory matches then create a new object if (findIfNameExist === -1) { let obj = { 'NamaCategory': curr.NamaCategory, "value": [curr] } acc.push(obj) } else { // if name category matches , then push the value acc[findIfNameExist].value.push(curr) } return acc; }, []); console.log(newArray)
var data = [{"value":"Osteonecrosis","Diagnosis_Code":"DIAG002","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG002","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19},{"value":"Osteonecrosis","Diagnosis_Code":"DIAG004","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG004","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19}]; var final = []; data.forEach(function(e) { var match = false; final.forEach(function(i) { if (e.NamaCategory == i.NamaCategory) { match = true; } }); if (!match) { var obj = { NamaCategory: e.NamaCategory, values: [e] } final.push(obj); } else { final.forEach(function(i) { if (e.NamaCategory == i.NamaCategory) { i.values.push(e); } }); } }); console.log(final);
Just loop through data and check that if element exists in final array, If exists push the value to values property, if not create a new property in the final array. 只需遍历数据并检查if final是否存在于最终数组中,If exists将值推送到values属性,如果不在最终数组中创建新属性。
let data = [ { "value": "Osteonecrosis", "Diagnosis_Code": "DIAG002", "NamaCategory": "Primary Category", "FK_Diagnosis_Content_ID": 2 }, { "value": "Malunion", "Diagnosis_Code": "DIAG002", "NamaCategory": "Healing", "FK_Diagnosis_Content_ID": 19 }, { "value": "Osteonecrosis", "Diagnosis_Code": "DIAG004", "NamaCategory": "Primary Category", "FK_Diagnosis_Content_ID": 2 }, { "value": "Malunion", "Diagnosis_Code": "DIAG004", "NamaCategory": "Healing", "FK_Diagnosis_Content_ID": 19 } ]; //grouping by name: //first creating a map by name for that let dataByNamaCategory = {}; //iterating over the input array and using object destructuring to seperate the name from the other props data.forEach(({NamaCategory, ...otherProps}) => { //if there's already an entry by this name in the map if(NamaCategory in dataByNamaCategory){ //just push the new value dataByNamaCategory[NamaCategory].value.push(otherProps) }else{ //otherwise create a new entry on the map dataByNamaCategory[NamaCategory] = { NamaCategory, value: [ otherProps ] }; } }); //get just the values from the map let groupedData = Object.values(dataByNamaCategory); console.log(groupedData);
.as-console-wrapper{top:0;max-height:100%!important}
I've commented the code. 我评论了代码。 Does this require any further explanation besides the comments in the code?
除了代码中的注释之外,这还需要进一步解释吗? Well, you'd have to get familiar with object destructuring
好吧,你必须熟悉对象解构
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.