比较数组并将值连接在一起

问题描述 投票:0回答:1

来源 JSON:

{
  "query": {
    "ids": [
      44911681
    ],
    "dimensions": [
      "ym:s:date",
      "ym:s:cross_device_last_significantUTMSource",
      "ym:s:cross_device_last_significantUTMMedium",
      "ym:s:cross_device_last_significantUTMCampaign"
    ],
    "metrics": [
      "ym:s:goal3044911681reaches",
      "ym:s:goal3044911681visits",
      "ym:s:goal5130447458reaches",
      "ym:s:goal5130447458visits"
    ],
    "date1": "2024-10-01",
    "date2": "2024-10-05"
  },
  "data": [
    {
      "dimensions": [
        {
          "name": "2024-10-03"
        },
        {
          "name": "Yandex_network"
        },
        {
          "name": "cpc"
        },
        {
          "name": "Spar_october_2024"
        }
      ],
      "metrics": [
        5,
        4,
        12,
        7
      ]
    },
    {
      "dimensions": [
        {
          "name": "2024-10-04"
        },
        {
          "name": "Yandex_network"
        },
        {
          "name": "cpc"
        },
        {
          "name": "Spar_october_2024"
        }
      ],
      "metrics": [
        5,
        5,
        3,
        9
      ]
    }
  ],
  "total_rows": 2
}

来自我需要的源 JSON:

    来自
  • id
     数组的 
    query.ids
    并调用新属性
    counter_id
  • 比较并连接数组
    query.dimensions
    中的值和数组
    data.dimensions
    中的值,但给它们新名称。 query.dimensions 中的值应该是
    data.dimensions
    中的值的属性名称(我们需要它来保持正确的顺序)。之后我想更改这些名称:
    ym:s:date
    应该是
    date_of_visit
    ym:s:lastSignUTMSource
    应该是
    utm_source
    ym:s:lastSignUTMMedium
    utm_medium
    ym:s:lastSignUTMCampaign
    utm_campaign
  • query.metrics
    中的值与数组
    data.metrics
    中的值进行比较并连接。但数组
    query.metrics
    中的每个元素,例如 ym:s:goal304491168visits (或 goal3044911681reaches)应写为 goal_id: 304491168。数组
    data.metrics
    中的元素应写为: 1) as
    conversions
    (其中指标名称以访问次数结尾)2)为
    goal_reaches
    (其中指标名称以到达次数结尾)

所以,根据这个 JSON 和这个描述,我期望:

[
   {
      "counter_id":44911681,
      "date_of_visit":"2024-10-03",
      "utm_source":"Yandex_network",
      "utm_medium":"cpc",
      "utm_campaign":"Spar_october_2024",
      "goal_id":3044911681,
      "conversions":5,
      "goal_reaches":4
   },
   {
      "counter_id":44911681,
      "date_of_visit":"2024-10-03",
      "utm_source":"Yandex_network",
      "utm_medium":"cpc",
      "utm_campaign":"Spar_october_2024",
      "goal_id":5130447458,
      "conversions":12,
      "goal_reaches":7
   },
   {
      "counter_id":44911681,
      "date_of_visit":"2024-10-04",
      "utm_source":"Yandex_network",
      "utm_medium":"cpc",
      "utm_campaign":"Spar_october_2024",
      "goal_id":3044911681,
      "conversions":5,
      "goal_reaches":5
   },
   {
      "counter_id":44911681,
      "date_of_visit":"2024-10-04",
      "utm_source":"Yandex_network",
      "utm_medium":"cpc",
      "utm_campaign":"Spar_october_2024",
      "goal_id":5130447458,
      "conversions":3,
      "goal_reaches":9
   }
]

我只是为了构建这个:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "metrics": {
            "*": {
              "@4,query.ids[0]": "&3_&.counter_id",
              "@2,dimensions[0].name": "&3_&.date_of_visit",
              "@2,dimensions[1].name": "&3_&.utm_source",
              "@2,dimensions[2].name": "&3_&.utm_medium",
              "@2,dimensions[3].name": "&3_&.utm_campaign"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

如何从

id
添加这些指标和
goal...visits

json jolt
1个回答
0
投票

OP 可能会查看例如

flatMap
以及解构赋值,包括变量别名/重命名以及扩展语法

所有上述技术均由下一个提供且注释良好的示例代码使用...

function createUnfoldedDimensionData({ query, data: dimensionData }) {

  // - extract all metadata first.
  const counter_id = query.ids.at(0);
  const goal_ids = [

    ...query.metrics

      // - create a single '-' separated string value from all array entries.
      .join(',')

      // match any `reaches` related pattern with following matching regex.
      .matchAll(/goal(\d+)reaches(?:,|$)/g)

      // - as for the regex ... see ... [https://regex101.com/r/4QsJIT/1].
  ]
  // - extract the `id` value from each match of all matching items.
  .map(([_, id]) => id);

  // console.log({ counter_id, goal_ids });

  // - create and return the targeted data-structure ... by ...
  return dimensionData

    // ... iterating the dimension-specific `data`
    //     array of the profided data-source ...
    .flatMap(({dimensions, metrics}) => {

      // ... creating a dimension-specific base-item for
      //     each of the `data`-array's iteration-steps ...
      const baseItem = {
        counter_id: 44911681,

        date_of_visit: dimensions.at(0).name,
        utm_source: dimensions.at(1).name,
        utm_medium: dimensions.at(2).name,
        utm_campaign: dimensions.at(3).name,
      };
      // ... creating and returning two concrete
      //     dimension-specific items for each of
      //     the `data`-array's iteration-steps ...
      return [{
        ...baseItem,

        goal_id: goal_ids.at(0),
        conversions: metrics.at(0),
        goal_reaches: metrics.at(1),
      }, {
        ...baseItem,

        goal_id: goal_ids.at(1),
        conversions: metrics.at(2),
        goal_reaches: metrics.at(3),
      }];
    });
}
const result = createUnfoldedDimensionData(sourceData);

console.log({ result });
.as-console-wrapper { min-height: 100%!important; top: 0; }
<script>
const sourceData = {
  query: {
    ids: [44911681],
    dimensions: ["ym:s:date", "ym:s:cross_device_last_significantUTMSource", "ym:s:cross_device_last_significantUTMMedium", "ym:s:cross_device_last_significantUTMCampaign"],
    metrics: ["ym:s:goal3044911681reaches", "ym:s:goal3044911681visits", "ym:s:goal5130447458reaches", "ym:s:goal5130447458visits"],
    date1: "2024-10-01",
    date2: "2024-10-05",
  },
  data: [{
    dimensions: [{ name: "2024-10-03" }, { name: "Yandex_network" }, { name: "cpc" }, { name: "Spar_october_2024" }],
    metrics: [5, 4, 12, 7],
  }, {
    dimensions: [{ name: "2024-10-04" }, { name: "Yandex_network" }, { name: "cpc" }, { name: "Spar_october_2024" }],
    metrics: [5, 5, 3, 9 ],
  }],
  total_rows: 2
};
</script>

© www.soinside.com 2019 - 2024. All rights reserved.