admin管理员组

文章数量:1335624

var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };

Is there an easier way than multiple loops to produce a result like follows:

{
    male: [10, 11, 12],
    female: [20, 21, 22],
    unassigned: [30, 31, 32]
}

I am not sure if 'bine' is the right word here.

var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };

Is there an easier way than multiple loops to produce a result like follows:

{
    male: [10, 11, 12],
    female: [20, 21, 22],
    unassigned: [30, 31, 32]
}

I am not sure if 'bine' is the right word here.

Share Improve this question asked Dec 4, 2019 at 2:53 Steven FloydSteven Floyd 3531 gold badge3 silver badges11 bronze badges 3
  • FYI, in JS we usually call these Objects, not dictionaries or lists :) – user7886229 Commented Dec 4, 2019 at 3:12
  • See also: stackoverflow./questions/59122448/… – JLRishe Commented Dec 4, 2019 at 4:26
  • Does this answer your question? How to group an array of objects by key – alexyorke Commented Dec 4, 2019 at 4:38
Add a ment  | 

5 Answers 5

Reset to default 6

This still requires a couple loops, but perhaps a bination of reduce and for...in will get this done eloquently:

var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };

const res = [dictA, dictB, dictC].reduce((acc, el) => {
  for (let key in el) {
    acc[key] = [...acc[key] || [], el[key]];
  };
  return acc;
}, {})

console.log(res);

This works assuming dictA has all of the necessary properties. It's not as functional like @Nicks.

const dictA = { male: 10, female: 20, unassigned: 30 };
const dictB = { male: 11, female: 21, unassigned: 31 };
const dictC = { male: 12, female: 22, unassigned: 32 };
    
const obj = {};

Object.keys(dictA).forEach(key => {
   obj[key] = [dictA,dictB,dictC].map(dict => dict[key]);
});

console.log(obj);

var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };

var input = [dictA, dictB, dictC];
var output = [];
input.forEach(function(item) {
  var existing = output.filter(function(v, i) {
    return v.name == item.name;
  });
  if (existing.length) {
    var existingIndex = output.indexOf(existing[0]);
    // output[existingIndex].value = output[existingIndex].value.concat(item.value);
  } else {
    if (typeof item.value == 'string')
      item.value = [item.value];
    output.push(item);
  }
});

alert(output[0].male);

You could do it with .reduce() and .forEach() too

let result = [dictA, dictB, dictC].reduce((rv, dict) => (
    Object.keys(dict).forEach(key => (
        rv[key] != null ? rv[key].push(dict[key]) : rv[key] = [dict[key]]
    )),
    rv
), {});

const addToBucket = (bucket, [k, v], prev = bucket[k] || []) => ({
  ...bucket, [k]: [...prev, v]
})  

const bucket = list => list.flatMap(Object.entries).reduce(
  (bucket, entry) => addToBucket(bucket, entry), {}
)

// ---------------------------------------------------------- //
console.log(bucket([
  { male: 10, female: 20, unassigned: 30 },
  { male: 11, female: 21, unassigned: 31 },
  { male: 12, female: 22, unassigned: 32 },
]))

本文标签: listHow to combine multiple dictionaries in JavascriptStack Overflow