如何将2个或多个API来源合并到一个表格中(React,HTML)

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

How to combine 2 or more API sources into one table (React, HTML)

问题

所以我有3个API来源,以这种格式返回数据

firstSource = [{month, endDate, expectedPrice}] ->12个对象的数组
secondSource = [{month, value}] ->12个对象的数组
thirdSource = [{month, value}] ->12个对象的数组

例子:

secondSource = [
{month: 1, value: 234.22},
{month: 2, value: 123.58},
{month: 3, value: 255.35},
...
...
你明白我的意思吧
]

我找到了这个解决方案,它使用了lodash库。这都很好,但我对一个不需要外部库的解决方案感兴趣。所以基本上,我想要达到与lodash解决方案中的人相同的目标 -> 将所有三个数据源合并到一个数据源中,然后使用它来在<table>元素中显示数据,只是不使用外部库,如lodash。

期望的输出:

combined = [
{month, endDate, expectedPrice, secondSourceValue, thirdSourceValue}
]

有什么想法?

英文:

So I have 3 API sources that return data in this format

firstSource = [{month, endDate, expectedPrice}] ->array of 12 objects
secondSource = [{month, value}] ->array of 12 objects
thirdSource = [{month, value}] ->array of 12 objects

example:

secondSource = [
{month: 1, value: 234.22},
{month: 2, value: 123.58},
{month: 3, value: 255.35},
...
...
you get the idea
]

I've come across this solution that uses lodash library. This is all well, but I'm interested in a solution that doesn't require external libraries. So basically, I want to achieve the same thing the person from the lodash solution did -> combine all three data sources into one and then use that to display the data in <table> element, only without using external libraries, e.g. lodash.

Expected output:

combined = [
{month, endDate, expectedPrice, secondSourceValue, thirdSourceValue}
]

Any ideas?

答案1

得分: 1

你只需检查第二个和第三个来源的长度是否与主要来源相同。

英文:

what's the problem to try something like this?

let combinedData = []

firstSource.forEach((element, index) => {
    combinedData[index] = {
      month: element.month,
      endDate: element.endDate,
      expectedPrice: element.expectedPrice,
      secondSourceValue: secondSource[index].value,
      thirdSourceValue: thirdSource[index].value
}    
});

You only have to check if the second and third sources have the same length with the main.

答案2

得分: 0

我们可以使用 array.map 来合并多个对象数组,并创建一个包含所有对象属性的新数组。

需要注意的是,在这种情况下,所有数组的长度应该是相同的。

以下是 JavaScript 代码示例:

let arr1 = [
    {
        month: 1,
        value: 234.22,
        date: 'JAN'
    },
    {
        month: 2,
        value: 123.58,
        date: 'FEB'
    },
    {
        month: 3,
        value: 255.35,
        date: 'MARCH'
    }
]

let arr2 = [
    {
        month: 1,
        property: 1
    },
    {
        month: 2,
        property: 2
    },
    {
        month: 3,
        property: 3
    }
]

let arr3 = [
    {
        month: 1,
        property: 9
    },
    {
        month: 2,
        property: 8
    },
    {
        month: 3,
        property: 7
    },
]

let combinedArr = arr1.map((item, index) => {
    return {
        ...item,
        ...arr2[index],
        ...arr3[index]
    }
})

console.log(combinedArr)

这段代码会将 arr1arr2arr3 的对象数组合并为一个新的数组 combinedArr,每个对象包含来自所有原始数组的属性。

英文:

We can use array.mapto merge multiple arrays of objects and create a new array with properties of all objects.

Do note that, in this case, the length of all the arrays should be the same.

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

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

let arr1 = [{
    month: 1,
    value: 234.22,
    date: &#39;JAN&#39;
  },
  {
    month: 2,
    value: 123.58,
    date: &#39;FEB&#39;
  },
  {
    month: 3,
    value: 255.35,
    date: &#39;MARCH&#39;
  }
]

let arr2 = [{
    month: 1,
    property: 1
  },
  {
    month: 2,
    property: 2
  },
  {
    month: 3,
    property: 3
  }
]

let arr3 = [{
    month: 1,
    property: 9
  },
  {
    month: 2,
    property: 8
  },
  {
    month: 3,
    property: 7
  },
]


let combinedArr = arr1.map((item, index) =&gt; {
  return {
    ...item,
    ...arr2[index],
    ...arr3[index]
  }
})

console.log(combinedArr)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 19:40:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471742.html
匿名

发表评论

匿名网友

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

确定