将重复的对象值转换为数组Javascript

时间:2018-05-08 06:18:08

标签: javascript json

我花了几个小时才找到处理这个对象数组的方法:

[
  {
    "value": "foo01",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo02",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo03",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo04",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo03",
    "name": "bar2"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo01",
    "name": "bar2"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo02",
    "name": "bar2"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo04",
    "name": "bar2"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo01",
    "name": "bar3"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo03",
    "name": "bar3"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo04",
    "name": "bar3"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo02",
    "name": "bar3"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo01",
    "name": "bar4"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo02",
    "name": "bar4"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo04",
    "name": "bar4"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo03",
    "name": "bar4"
  }
]

我的预期JSON是这样的:

[
  {
    "name":"bar",
    "value":[
      {
        "value": "foo01",
        "Diagnosis_Code": ["Tidak ada"],
        "name": "bar",
      },
      {
        "value": "foo02",
        "Diagnosis_Code": ["Tidak ada"],
        "name": "bar",
      },
      {
        "value": "foo03",
        "Diagnosis_Code": ["Tidak ada"],
        "name": "bar",
      },
      {
        "value": "foo04",
        "Diagnosis_Code": ["Tidak ada"],
        "name": "bar",
      }
    ]
  },
  {
    "name": "bar2",
    "value": [
      {
        "value": "val1",
        "Diagnosis_Code": ["foo03"],
        "name": "bar2",
      },
      {
        "value": "val2",
        "Diagnosis_Code": ["foo01"],
        "name": "bar2",
      },
      {
        "value": "val3",
        "Diagnosis_Code": ["foo02", "foo04"],
        "name": "bar2",
      }
    ]
  },
  {
    "name": "bar3",
    "value":[
      {
        "value": "val1",
        "Diagnosis_Code": ["foo01"],
        "name": "bar3",
      },
      {
        "value": "val2",
        "Diagnosis_Code": ["foo03", "foo04"],
        "name": "bar3",
      },
      {
        "value": "val3",
        "Diagnosis_Code": ["foo02"],
        "name": "bar3",
      },
    ]
  },
  {
    "name": "bar4",
    "value": [
      {
        "value": "val1",
        "Diagnosis_Code": ["foo01", "foo02", "foo04"],
        "name": "bar4",
      },
      {
        "value": "val2",
        "Diagnosis_Code": ["foo03"],
        "name": "bar4",
      },
    ]
  },
]

我想将重复name值放入Object,如果json的Diagnosis_Code与另一个值相同,则将其放入数组

我现在使用的代码只是使用reduce来处理name,但我不知道如何处理Diagnosis_Code属性:

var orgArray = [{"value":"foo01","Diagnosis_Code":"Tidak ada","name":"bar"},{"value":"foo02","Diagnosis_Code":"Tidak ada","name":"bar"},{"value":"foo03","Diagnosis_Code":"Tidak ada","name":"bar"},{"value":"foo04","Diagnosis_Code":"Tidak ada","name":"bar"},{"value":"val1","Diagnosis_Code":"foo03","name":"bar2"},{"value":"val2","Diagnosis_Code":"foo01","name":"bar2"},{"value":"val3","Diagnosis_Code":"foo02","name":"bar2"},{"value":"val3","Diagnosis_Code":"foo04","name":"bar2"},{"value":"val1","Diagnosis_Code":"foo01","name":"bar3"},{"value":"val2","Diagnosis_Code":"foo03","name":"bar3"},{"value":"val2","Diagnosis_Code":"foo04","name":"bar3"},{"value":"val3","Diagnosis_Code":"foo02","name":"bar3"},{"value":"val1","Diagnosis_Code":"foo01","name":"bar4"},{"value":"val1","Diagnosis_Code":"foo02","name":"bar4"},{"value":"val1","Diagnosis_Code":"foo04","name":"bar4"},{"value":"val2","Diagnosis_Code":"foo03","name":"bar4"}];

var newArray = orgArray.reduce(function(acc, curr) {
  var findIfNameExist = acc.findIndex(function(item) {
    return item.name === curr.name;
  })
  if (findIfNameExist === -1) {
    let obj = {
      'name': curr.name,
      "value": [curr]
    }
    acc.push(obj)
  } else {
    acc[findIfNameExist].value.push(curr)
  }
  return acc;

}, []);
console.log(newArray)

有没有办法处理将重复的Diagnosis_Code转换为数组?

2 个答案:

答案 0 :(得分:2)

这是另一个例子!

首先,我使用reduce来减少你的对象,然后,我使用map来重新映射它以获得预期的格式;)

这可能不是最佳性能方式,但我发现它是更易于维护和可读的方式。

我希望这很明确,并会帮助你:D



const input = [
  {
    "value": "foo01",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo02",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo03",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo04",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo03",
    "name": "bar2"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo01",
    "name": "bar2"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo02",
    "name": "bar2"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo04",
    "name": "bar2"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo01",
    "name": "bar3"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo03",
    "name": "bar3"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo04",
    "name": "bar3"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo02",
    "name": "bar3"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo01",
    "name": "bar4"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo02",
    "name": "bar4"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo04",
    "name": "bar4"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo03",
    "name": "bar4"
  }
];

// Condensed/reduced object.
let output = input.reduce((accum, item) => {
  if(!accum[item.name]) {
    accum[item.name] = {};
  }
  if(!accum[item.name][item.value]) {
    accum[item.name][item.value] = [];
  }
  accum[item.name][item.value].push(item.Diagnosis_Code);
  accum[item.name][item.value].original = item;
  return accum;
}, {});

// Object remapped to fit your expected format.
output = Object.keys(output).map((itemName) => {
  return {
    'name': itemName,
    'value': Object.keys(output[itemName]).map((valueName) => {
    const patchedOriginalItem = output[itemName][valueName].original;
    patchedOriginalItem['Diagnosis_Code'] = output[itemName][valueName];
      return patchedOriginalItem;
    })
  };
});

console.log(output);




答案 1 :(得分:1)

使用.reduce将数组转换为对象,然后您需要.find两次查看匹配的name然后匹配value对象存在。 find(或.findIndex)只是一次赢得不够。



const input = [
  {
    "value": "foo01",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo02",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo03",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "foo04",
    "Diagnosis_Code": "Tidak ada",
    "name": "bar"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo03",
    "name": "bar2"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo01",
    "name": "bar2"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo02",
    "name": "bar2"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo04",
    "name": "bar2"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo01",
    "name": "bar3"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo03",
    "name": "bar3"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo04",
    "name": "bar3"
  },
  {
    "value": "val3",
    "Diagnosis_Code": "foo02",
    "name": "bar3"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo01",
    "name": "bar4"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo02",
    "name": "bar4"
  },
  {
    "value": "val1",
    "Diagnosis_Code": "foo04",
    "name": "bar4"
  },
  {
    "value": "val2",
    "Diagnosis_Code": "foo03",
    "name": "bar4"
  }
];

const output = input.reduce((accum, item) => {
  const { name } = item;
  let foundNameObj = accum.find(({ name: findName }) => findName === name);
  if (!foundNameObj) {
    foundNameObj = { name, value: [] };
    accum.push(foundNameObj);
  }
  const foundValObj = foundNameObj.value.find(({ value }) => value === item.value);
  if (!foundValObj) {
    // value hasn't appeared before, we can just push the item:
    foundNameObj.value.push({...item, Diagnosis_Code: [item.Diagnosis_Code] });
    return accum;
  }
  foundValObj.Diagnosis_Code.push(item.Diagnosis_Code);
  return accum;
}, [])
console.log(output);




不要混用varlet / const - 如果你正在使用ES6,你应该使用const,除了当您需要重新分配时,使用let。 (var被提升并且具有功能范围而不是阻止范围,这是不直观的并且可能导致意外错误)