JS - 如何将相同的对象值推送到数组中

时间:2018-05-07 05:11:08

标签: javascript json

我已经尝试了几个小时来找到一种处理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
  }
]

我想在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
      }
    ]
  }
]

我对处理JSON并不熟悉,所以我需要帮助,

任何人都可以帮助我如何处理这些json?

3 个答案:

答案 0 :(得分:2)

使用reduce方法。这种情况返回一个新数组,同时创建这个新的对象数组,使用NamaCategory检查是否存在一个名称与旧数组的findIndex相同的对象。如果新数组中不存在findIndex,则-1将返回NamaCategory。如果它不存在,请创建具有所需值的对象并将其推送到新数组。如果存在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)




答案 1 :(得分:0)



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);




只需遍历数据并检查if element是否存在于最终数组中,If exists将值推送到values属性,如果不在最终数组中创建新属性。

答案 2 :(得分:0)



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}




我对代码进行了评论。除了代码中的注释之外,这还需要进一步解释吗?那么,你必须熟悉object destructuring