合并和转换 JavaScript 中的对象数组

huangapple go评论64阅读模式
英文:

Merging and Tranforming Array of object in javascripts

问题

我有两个对象数组,如下所示...

const planned = [
    {
        '2023-01-06': 46,
        '2023-01-04': 45,
        '2023-01-05': 43,
        '2023-01-07': 53
    }
]
const actual = [
    {
        "2023-01-07": 12,
        "2023-01-06": 16,
        "2023-01-04": 14,
        "2023-01-08": 10,
        "2023-01-05": 12,
        "2023-01-03": 10
    }
]
我尝试将它们合并成一个包含日期和值的对象数组这是我期望的数组:

```javascript
const transformed =  [{
    date: "2023-01-03",
    actual: 10,
    planned: 0,
  },{
    date: "2023-01-05",
    actual: 12,
    planned: 43,
  },{
    date: "2023-01-06",
    actual: 16,
    planned: 46,
  },....
]
这是我的尝试:

```javascript
const transformed = planned.map((el)=>{
  let obj ={}
  actual.map((els)=>{
     if(el.key === els.key){
       obj["date"]=== el.key
       obj["actual"]=== el.keys
       obj["planned"]=== el.key
     }
  })
 return {...obj}
})
我尝试弄清楚但在如何创建逻辑方面有一些问题... 有人能给我一些提示吗我们可以使用reduce原型数组或任何内置的方法吗对此的任何帮助都将非常感激
英文:

So i have two array of object like this...

const planned = [
    {
        '2023-01-06': 46,
        '2023-01-04': 45,
        '2023-01-05': 43,
        '2023-01-07': 53
    }
]
const actual =[
    {
        "2023-01-07": 12,
        "2023-01-06": 16,
        "2023-01-04": 14,
        "2023-01-08": 10,
        "2023-01-05": 12,
        "2023-01-03": 10
    }
]

i try to merge it into one array of object that separated the date and the value, here is my expected array:

const transformed =  [{
    date:"2023-01-03",
    actual:10,
    planned:0,
  },{
    date:"2023-01-05",
    actual:10,
    planned:5,
  },{
    date:"2023-01-06",
    actual:16,
    planned:46,
  },....

]
here is my try:

const transformed = planned.map((el)=>{
  let obj ={}
  actual.map((els)=>{
     if(el.key === els.key){
       obj["date"]=== el.key
       obj["actual"]=== el.keys
       obj["planned"]=== el.key
     }
  })
 return {...obj}
})

i try to figure it out, but have some issues on how to create the logic... anyone here to give me clue on it? can we use reduce prototype array or any built in?, any help on this will be verry thankful

答案1

得分: 2

这是一种方法。 plannedactual 是数组,所以我们首先将它们转换为一个扁平映射,然后使用这个映射创建输出。

const planned = [
    {
        "2023-01-06": 46,
        "2023-01-04": 45,
        "2023-01-05": 43,
        "2023-01-07": 53,
    },
];
const actual = [
    {
        "2023-01-07": 12,
        "2023-01-06": 16,
        "2023-01-04": 14,
        "2023-01-08": 10,
        "2023-01-05": 12,
        "2023-01-03": 10,
    },
];

const convertToMap = (list) =>
    list.reduce((acc, curr) => ({ ...acc, ...curr }), {});

const actualStats = convertToMap(actual);
const plannedStats = convertToMap(planned);

const dates = [
    ...new Set([...Object.keys(actualStats), ...Object.keys(plannedStats)]),
];

const transformed = dates.map((date) => ({
    date,
    planned: plannedStats[date] || 0,
    actual: actualStats[date] || 0,
}));

console.log(transformed);

如果您需要更多帮助,请告诉我。

英文:

Here's an approach. planned and actual are arrays, so we first convert them to a flat map and then use this map to create the output

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const planned = [
    {
        &quot;2023-01-06&quot;: 46,
        &quot;2023-01-04&quot;: 45,
        &quot;2023-01-05&quot;: 43,
        &quot;2023-01-07&quot;: 53,
    },
];
const actual = [
    {
        &quot;2023-01-07&quot;: 12,
        &quot;2023-01-06&quot;: 16,
        &quot;2023-01-04&quot;: 14,
        &quot;2023-01-08&quot;: 10,
        &quot;2023-01-05&quot;: 12,
        &quot;2023-01-03&quot;: 10,
    },
];

const convertToMap = (list) =&gt;
    list.reduce((acc, curr) =&gt; ({ ...acc, ...curr }), {});

const actualStats = convertToMap(actual);
const plannedStats = convertToMap(planned);

const dates = [
    ...new Set([...Object.keys(actualStats), ...Object.keys(plannedStats)]),
];

const transformed = dates.map((date) =&gt; ({
    date,
    planned: plannedStats[date] || 0,
    actual: actualStats[date] || 0,
}));

console.log(transformed);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 12:57:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定