2

I have an array with repeating values:

[ ['Deer Skin', 1], ['Bear Skin', 1], ['Deer Skin', 1], ['Cougar Skin', 2] ... ]

I want to compare them by the sting value of index[0] and remove duplicates while storing the result in a new object.

The resulting values would look like:

[ {name: 'Deer Skin', quantity: 4}, {name: 'Bear Skin', quantity: 5}, {name: 'Cougar Skin', quantity: 4} ... ]

I'm not entirely sure how to proceed from here. Right now I have an array of objects with all initial duplicate array values removed:


[
{name: "Deer Skin", quantity: 0}
{name: "Bear Skin", quantity: 0}
{name: "Cougar Skin", quantity: 0}
...

But I'm not understanding how I can map the values of:

[ ['Deer Skin', 1], ['Bear Skin', 1], ['Deer Skin', 1], ['Cougar Skin', 2] ... ]

to above object.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Kyle Pfahler
  • 45
  • 1
  • 3

3 Answers3

2

You can use .reduce to achieve this. Here I have initialized the accumulator (acc) to be an empty object {}. As the array is iterated, the ternary operator decides whether to add the quantity to the current value of the key (elem[0]) (we only add if we have seen it before - if we tried to add to something we hadn't of seen before we would get NaN as our value) or whether to create a new key-value pair (if we haven't seen the key value before).

See working example below:

const arr = [
  ['Deer Skin', 1],
  ['Bear Skin', 1],
  ['Deer Skin', 1],
  ['Cougar Skin', 2]
];

const res = arr.reduce((acc, elem) => {
  elem[0] in acc ? acc[elem[0]] += elem[1] : acc[elem[0]] = elem[1];
  return acc;
}, {});

console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

Using Object.entries, array.reduce, and array destructuring:

const data = [
  ['Deer Skin', 1],
  ['Bear Skin', 1],
  ['Deer Skin', 1],
  ['Cougar Skin', 2]
];

const reduceData = (data) => data.reduce((acc, [key, count]) => {
  if (!acc[key]) acc[key] = 0; // If key doesn't exist yet, add and set count to zero
  acc[key] += count;
  return acc;
}, {});

console.log(reduceData(data));

// But if you want an array, use Object.entries
console.log(Object.entries(reduceData(data)));
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
0

Since you haven't mentioned the structure of the new object in which you're going to store the uniques, you could do something like this:

const arrayItems = [ ['Deer Skin', 1], ['Bear Skin', 1], ['Deer Skin', 2], ['Cougar Skin', 2] ... ];
const deduplicatedObj = {};

for(let item of arrayItems){
    deduplicatedObj[item[0]] = item[1];
}

console.log(deduplicatedObj); // This should be an object with key as 'Deer Skin' and value as 1, and so on. 

Since you are storing the value in an object, and an object can have only unique keys, when you assign item[1] to deduplicatedObj[item[0]], it will overwrite the pre-existing value if any.

The deduplicatedObj will look something like this:

deduplicatedObj = {
    'Deer Skin': 2,
    'Bear Skin': 1,
    'Cougar Skin': 2
}
asleepysamurai
  • 1,362
  • 2
  • 14
  • 23