Skip to content Skip to sidebar Skip to footer

Aggregate Same Key Values Into An Array And Avoid Undefined

I am trying to aggregate the same key values into an array by value. so for example I have an array of objects, like so const data = [{foo: true},{foo: false},{bar: true},{buzz: fa

Solution 1:

You were close, you just need to check if the property you were adding was undefined before adding. You can also check if the reduced object has any properties before adding to the result object.

Note that this may not be the most efficient way of doing it, but sometimes it's better to understand the code than it is to have highly efficient code.

const data = [{
  foo: true
}, {
  foo: false
}, {
  bar: true
}, {
  buzz: false
}];
let v = [];
let types = ['foo', 'bar', 'buzz', 'fizz']
for (let x = 0; x < types.length; x++) {
  let reduced = data.reduce((acc, i) => {
    //                        /* Added this type check            */if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
      acc[i[types[x]]] = [i]
    } elseif (Array.isArray(acc[i[types[x]]])) {
      acc[i[types[x]]].push(i);
    } elseif (typeof acc[i[types[x]]] === 'object') {
      acc[i[types[x]]] = [acc[i[types[x]]]]
      acc[i[types[x]]].push(i)
    }
    return acc;
  }, {});
  // Doesn't add a property for the type if there are no dataif (Object.keys(reduced).length) {
    v.push({
      [types[x]]: reduced
    });
  }
}

console.log(v);

Solution 2:

Have a look at how Array.prototype.reduce works. It might be the right method to build your approach upon.

A generic way of solving the OP's problem was to iterate the provided data array. For each item one would extract its key and value. In case the item's key is listed (included) in another provided types array, one would continue creating a new data structure and collecting the currently processed item within the latter.

One does not want to iterate the types array for it will cause a unnecessarily complex lookup for the data items, each time a type item is going to be processed.

Thus a generically working (better code reuse) reduce method might be the best solution to the OP's problem ...

const sampleDataList = [
  { foo: true },
  { foo: false },
  { bar: true },
  { baz: false },
  { buzz: false },
  { baz: false },
  { bar: true }
];

// foo: {true: [{foo: true}], false: [{foo: false}]},// bar: {true: [{bar: true}]},// buzz: {false: [{buzz: false}]}functioncollectItemIntoInclusiveKeyValueGroup(collector, item) {
  const { inclusiveKeyList, index } = collector;
  const firstItemEntry = Object.entries(item)[0];

  const key = firstItemEntry[0];
  const isProceedCollecting = (       // proceed with collecting ...//
    !Array.isArray(inclusiveKeyList)  // - either for no given list
    || inclusiveKeyList.includes(key) // - or if item key is listed.
  );
  if (isProceedCollecting) {

    let keyGroup = index[key];        // access the group identifiedif (!keyGroup) {                  // by an item's key, ... or ...// ...create it in case ...
      keyGroup = index[key] = {};     // ...it did not yet exist.
    }
    const valueLabel = String(firstItemEntry[1]); // item value as key.let valueGroupList = keyGroup[valueLabel];    // acces the group listif (!valueGroupList) {                        // identified by an item's// value, ...or create it in
      valueGroupList = keyGroup[valueLabel] = []; // case it did not yet exist.
    }
    // push original reference into a grouped// key value list, as required by the OP.
    valueGroupList.push(item);
  }
  return collector;
}

console.log(
  "'foo', 'bar', 'buzz' and 'fizz' only :",
  sampleDataList.reduce(collectItemIntoInclusiveKeyValueGroup, {

    inclusiveKeyList: ['foo', 'bar', 'buzz', 'fizz'],
    index: {}

  }).index
);
console.log(
  "'foo', 'bar' and 'baz' only :",
  sampleDataList.reduce(collectItemIntoInclusiveKeyValueGroup, {

    inclusiveKeyList: ['foo', 'bar', 'baz'],
    index: {}

  }).index
);
console.log(
  "all available keys :",
  sampleDataList.reduce(collectItemIntoInclusiveKeyValueGroup, {

    index: {}

  }).index
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Solution 3:

Try something like:

const data = [{foo: true},{foo: false},{bar: true},{buzz: false}];
    let v = [];
    let types = ['foo', 'bar', 'buzz', 'fizz' ];
        for (let x = 0; x < types.length; x++) {
          let filteredlist = data.filter(function (d) {
            returnObject.keys(d)[0] == types[x];
          });
          let isTrue = 0;
          let isFalse = 0;
          if (filteredlist.length > 0) {
            for (let i = 0; i < filteredlist.length; i++) {
              let trueOrfalse = eval("filteredlist[i]." + types[x]);
              if (trueOrfalse) {
                isTrue++;
              } else {
                isFalse++;
              }
            }
            v.push(types[x], {true: isTrue, false: isFalse});
          }
        }
    console.log(v);

Solution 4:

Assuming you only want to count the number of each key (e.g. true or false) you can use the following code.

I've written this as a function named 'aggregate' so that it can be called multiple times with different arguments.

const initialData = [{foo: true},{foo: true},{foo: false},{bar: true},{buzz: false}];
const types = ['foo', 'bar', 'buzz', 'fizz'];

constaggregate = (data, types) => {
  const result = {};

  data.forEach(item => {
    // Extract key & value from object// Note: use index 0 because each object in your example only has a single keyconst [key, value] = Object.entries(item)[0];
    // Check if result already contains this keyif (result[key]) {
      if (result[key][value]) {
        // If value already exists, append one
        result[key][value]++;
      } else {
        // Create new key and instantiate with value 1
        result[key][value] = 1;
      }
    } else {
      // If result doesn't contain key, instantiate with value 1
      result[key] = { [value]: 1 };
    }
  });
  
  return result;
};

console.log(aggregate(initialData, types));

This will output the following (note I've added another {foo: true} to your initialData array for testing).

The output should also be an object (not array) so that each key directly relates to its corresponding value, as opposed to an Array which will simply place the value as the next item in the Array (without explicitly linking the two).

{
  foo: { true:2, false:1 },
  bar: { true:1 },
  buzz: { false:1 }
}

Post a Comment for "Aggregate Same Key Values Into An Array And Avoid Undefined"