微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对于一组对象,我想找到一个相同属性的不同值的频率,但想将特定计数写入每个项目

如何解决对于一组对象,我想找到一个相同属性的不同值的频率,但想将特定计数写入每个项目

我有一个包含键和值的对象数组,但有时特定键的值不同。我想找到一个相同属性名称的不同值的频率。示例数据结构可能如下所示...

var input = [{
  "DPM": "Financing","Tracking_Block": "Capabilities","assessment_field": "channel_availability","assessment_field_values": "Yes","Field_Type": "Dropdown",},{
  "DPM": "Financing","assessment_field_values": "No","Tracking_Block": "Awareness","assessment_field": "Monthly_price_visibility","assessment_field_values": "Lead on all comms","assessment_field_values": "In buyflow marketing","assessment_field_values": "Financing as option at checkout",}];

找到所有特定于属性值的计数后,我想更改与特定计数相关的每个原始数组项,以便为每个相关属性和值计数添加一个额外的 Count 字段.

计算以下项目属性的不同值的频率... DPMTracking_Blockassessment_field ... 上面的原始样本数据将更改为 ...

[{
  "DPM": "Financing","DPM_Count": 5,"Tracking_Block_Count": 2,"assessment_field_Count": 2,"Tracking_Block_Count": 3,"assessment_field_Count": 3,}]

以下是我尝试进行分组和计数的代码

const result = Object.values(input.reduce( (acc,i) => {
    const key = i.DPM+ i. Tracking_Block + i. assessment_field;
    if(!acc[key])
       acc[key] = {...i,assessment_field_values: [i. assessment_field_values] }
    else
       acc[key]. assessment_field_values.push(i. assessment_field_values);
    return acc;
 },{}));

解决方法

一个好的方法是确定任务并分别实施。

因此,首先要弄清楚一个或多个属性的相同值发生的频率。

这一般可以通过一个可配置的 reduce 任务来解决,该任务读取和处理属性列表并计算所有属性的值。

function collectEntryCounts(config,item) {
  const { propertyList,counts } = config;

  // custom list of property names whose frequencies have to be counted.
  propertyList.forEach(propertyName => {

    // if the currently processed item features
    // one of the specified property names ...
    if (propertyName in item) {

      // ... get the property name specific count index
      // or create it in case it does not yet exist and ...
      const countIndex = counts[propertyName] || (counts[propertyName] = {});

      // ... get the current array item's property value ... 
      const propertyValue = item[propertyName];

      // ... which will be used as key for the property value specific count.
      if (propertyValue in countIndex) {

        countIndex[propertyValue]++;
      } else {
        countIndex[propertyValue] = 1;
      }
    }
  });

  return config;
}

var input = [{
  "DPM": "Financing","Tracking_Block": "Capabilities","assessment_field": "channel_availability","assessment_field_values": "Yes","Field_Type": "Dropdown",},{
  "DPM": "Financing","assessment_field_values": "No","Tracking_Block": "Awareness","assessment_field": "Monthly_price_visibility","assessment_field_values": "Lead on all comms","assessment_field_values": "In buyflow marketing","assessment_field_values": "Financing as option at checkout",}];

console.log(
  'property value counts index ...',input.reduce(collectEntryCounts,{

    propertyList: ['DPM','Tracking_Block','assessment_field'],counts: {},}).counts
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

在第二步中,现在可以通过向原始数组添加特定于键值的计数来更改/变异原始数组的每一项。

这也将通过一个通用实现来完成,例如 ... addEntryCountsFromBoundConfig ... 这是一个 forEach 任务,配置绑定到它作为 forEach's thisArgs 参数。

配置对象本身是之前运行 reducecollectEntryCounts 任务的结果。

function collectEntryCounts(config,counts } = config;

  // custom list of property names whose frequencies have to be counted.
  propertyList.forEach(propertyName => {

    // if the currently processed item features
    // one of the specified property names ...
    if (propertyName in item) {

      // ... get the property name specific count index
      // or create it in case it does not yet exist and ...
      const countIndex = counts[propertyName] || (counts[propertyName] = {});

      // ... get the current array item's property value ... 
      const propertyValue = item[propertyName];

      // ... which will be used as key for the property value specific count.
      if (propertyValue in countIndex) {

        countIndex[propertyValue]++;
      } else {
        countIndex[propertyValue] = 1;
      }
    }
  });

  return config;
}

function addEntryCountsFromBoundConfig(item) {
  const { propertyList,counts } = this; // `this` equals the bound config.

  propertyList.forEach(propertyName => {

    // if the currently processed item features one of the specified property
    // names and if the property name specific count index does exist too ...
    if ((propertyName in item) && counts[propertyName]) {

      // ... assign this count index from the bound config-properties and ...
      const countIndex = counts[propertyName];

      // ... do mutate the currently processed item by creating
      // the property specific count field and assigning its value.
      item[propertyName + '_Count'] = countIndex[item[propertyName]];
    }
  });
} 

var input = [{
  "DPM": "Financing",}];

input.forEach(
  // the callback function.
  addEntryCountsFromBoundConfig,// the this context bound to the above callback.
  input.reduce(collectEntryCounts,{
    propertyList: ['DPM',})
);
console.log('mutated `input` array ...',input);
.as-console-wrapper { min-height: 100%!important; top: 0; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。