let array = [{
hash: "11223344",
value: "abc"
},
{
hash: "11223344",
value: "def"
},
{
hash: "22113344",
value: "jkl"
},
{
hash: "22113344",
value: "zyw"
},
{
hash: "33221144",
value: "omn"
},
{
hash: "33221144",
value: "xyz"
}
];
let reducedArray = array.reduce(function(acc, curr) {
let getHashIndex = acc.findIndex(function(item) {
return item.hash === curr.hash;
})
if (getHashIndex === -1) {
let obj = {};
obj.hash = curr.hash;
obj.value = [];
obj.value.push(curr.value);
acc.push(obj)
} else {
acc[getHashIndex].value.push(curr.value)
}
return acc;
}, [])
console.log(reducedArray)
Created
September 24, 2018 15:18
-
-
Save andrit/c520609737458789ec5cc8b35e8d55d1 to your computer and use it in GitHub Desktop.
and I wanted to loop through that array and create a new array whereby each each hash is only listed once and each value that was listed with a given is added to an array at the key value in the object of the single hash, like this:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment