-3

I have two object of arrays, i want to process it as following output

var parts={"Glenn": [12,22,32],
"Ryan": [13,23,33],
"Steve K": [14],
"Jerom":[15,25,35],
};

var labor={
"Glenn": [12,22,32],
"Ryan": [13,23,33],
"Steve K": [14],
"Nike":[16,26,36]
};

//Target output   
[{person:"Glenn",count:6},
{person:"Ryan",count:6},
{person:"Steve K",count:2},
{person:"Jerom",count:3},
{person:"Nike",count:3}
];

Here count means the length of array regards to key and person is the key,If keys are matched then add their array length and assign it to count and key appear as person, if no match then just copy it to new array.Please help me how to achieve above array objects

Sherin Green
  • 308
  • 1
  • 3
  • 18

4 Answers4

2

Combine the two arrays and reduce them.

This should work:

var parts = {
  "Glenn": [12, 22, 32],
  "Ryan": [13, 23, 33],
  "Steve K": [14],
  "Jerom": [15, 25, 35],
};

var labor = {
  "Glenn": [12, 22, 32],
  "Ryan": [13, 23, 33],
  "Steve K": [14],
  "Nike": [16, 26, 36]
};

const combined = [...Object.entries(parts), ...Object.entries(labor)];
const result = combined.reduce((res, curr) => {
  const existing = res.find(e => e.person === curr[0]);
  if (existing) {
    existing.count += curr[1].length;
  } else {
    res.push({
      person: curr[0],
      count: curr[1].length
    })
  }
  return res;
}, []);
Roberto Zvjerković
  • 9,657
  • 4
  • 26
  • 47
1

You can create a set of all the unique keys from both objects, then iterate over that set producing a result object from the lengths of the corresponding elements from the parts and labor object:

var parts = {
  "Glenn": [12, 22, 32],
  "Ryan": [13, 23, 33],
  "Steve K": [14],
  "Jerom": [15, 25, 35],
};

var labor = {
  "Glenn": [12, 22, 32],
  "Ryan": [13, 23, 33],
  "Steve K": [14],
  "Nike": [16, 26, 36]
};

let keys = new Set([...Object.keys(parts), ...Object.keys(labor)]);
let result = {}
keys.forEach(k => result[k] = (result[k] || 0) + (parts[k] || []).length + (labor[k] || []).length);
console.log(result);
Nick
  • 138,499
  • 22
  • 57
  • 95
0

Refer to the following code that makes use of the for...in loop:

var parts = {
    Glenn: [12, 22, 32],
    Ryan: [13, 23, 33],
    "Steve K": [14],
    Jerom: [15, 25, 35]
};
var labor = {
    Glenn: [12, 22, 32],
    Ryan: [13, 23, 33],
    "Steve K": [14],
    Nike: [16, 26, 36]
};
var counts = {};
// iterate through the parts and keep count
for (let key in parts) {
    if (key in counts) {
        counts[key] += parts[key].length;
    } else {
        counts[key] = parts[key].length;
    }
}
// iterate through the labor and keep count
for (let key in labor) {
    if (key in counts) {
        counts[key] += labor[key].length;
    } else {
        counts[key] = labor[key].length;
    }
}
var res = [];
// iterate through the counts and convert to an array
for (let key in counts) {
    res.push({
        person: key,
        count: counts[key]
    })
}
Aplet123
  • 33,825
  • 1
  • 29
  • 55
0

Iterate both arrays to find the occurences and then increment the count

var parts = {
Glenn: [12, 22, 32],
Ryan: [13, 23, 33],
"Steve K": [14],
Jerom: [15, 25, 35]
};

var labor = {
Glenn: [12, 22, 32],
Ryan: [13, 23, 33],
"Steve K": [14],
Nike: [16, 26, 36]
};

var arr = [];
for (let [key, value] of Object.entries(parts)) {
arr.push({
    person: key,
    count: value.length
});
}
for (let [key, value] of Object.entries(labor)) {
const found = arr.find(elem => elem.person === key);
if (found) {
    found.count += value.length;
} else {
    arr.push({
        person: key,
        count: value.length
    });
}
}
console.log(arr);
joy08
  • 9,004
  • 8
  • 38
  • 73