在React中从一个数组构建表格

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

Building a table from an array in React

问题

我有一个包含不同行业薪资的数组。我需要计算并创建一个基于此数据的表格。我真的很困惑如何去做。以下是一个示例数据:

const data=[
['Euro','Tech'],
['USD','Tech'],
['GBX', 'Health'],
['Euro', 'Real Estate'],
['GBX', 'Real Estate']
]

现在我需要在表格中显示行业、薪资和总计,如下所示:

行业 Euro GBX USD 总计
Tech 1 0 1 2
Health 0 1 0 1
Real Estate 1 1 0 2
总计 2 2 1 5

你能帮助我吗?我正在使用 React 表格来显示数据。

英文:

I have an array stating salary of different sectors. I need to calculate and then create a table based on this . I'm really confused how to do it . Here is an example data

const data=[
['Euro','Tech'],
['USD','Tech'],
['GBX', 'Health'],
['Euro', 'Real Estate'], 
['GBX', 'Real Estate'].
]

Now I have to display the sector ,salary & total in the Table like this below:

Sector Euro GBX USD Total
Tech 1 0 1 2
Health 0 1 0 1
Real Estate 1 1 0 2
Total 2 2 1 5

Could you help me with this. I'm using React table to display the data.

答案1

得分: 1

这里的想法是创建一个二维数组,用零填充,然后遍历数据数组中的每个项目,根据将部门名称映射到索引的映射和将货币名称映射到索引的映射,增加二维数组中的正确值。

希望这有所帮助,尽管我觉得可能有更好的方法来实现这个。

const data = [
  ['Euro','Tech'],
  ['USD','Tech'],
  ['GBX', 'Health'],
  ['Euro', 'Real Estate'], 
  ['GBX', 'Real Estate'],
  ['GBX', 'Health'],
];

const array_unique = (arr) => Array.from(new Set(arr));

const sectors = array_unique( data.map(([_, sector]) => sector) );
const currencies = array_unique( data.map(([currency]) => currency) ).sort();

const sector_map = Object.fromEntries(
  sectors.map((sector, index) => [sector, index])
);

const currency_map = Object.fromEntries(
  currencies.map((currency, index) => [currency, index])
);

// 用零填充二维数组
const values_matrix = sectors.map( () => currencies.map(() => 0) );

// 根据部门映射和货币映射,增加数据数组中每个项目的值
for(const [currency, sector] of data) {
  values_matrix[ sector_map[sector] ][ currency_map[currency] ]++;
}

// 为表格创建行
const rows = [['Sector', ...currencies]];
for(const [index, sector] of sectors.entries()) {
  rows.push([sector, ...values_matrix[index]]);
}

console.log(rows.map((row) => row.join(',')));

以上是代码的翻译部分,希望对你有所帮助。

英文:

Here the idea is to create a two dimensional array filled with zeros, then work through each item in the data array and increment the correct value in the two diminsional array based on a lookup using a map of sector names to indexes and a map of currency names to indexes.

Hopefully it helps, though I feel like there's probably a better way to do it.

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

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

const data = [
  [&#39;Euro&#39;,&#39;Tech&#39;],
  [&#39;USD&#39;,&#39;Tech&#39;],
  [&#39;GBX&#39;, &#39;Health&#39;],
  [&#39;Euro&#39;, &#39;Real Estate&#39;], 
  [&#39;GBX&#39;, &#39;Real Estate&#39;],
  [&#39;GBX&#39;, &#39;Health&#39;],
];

const array_unique = (arr) =&gt; Array.from(new Set(arr));

const sectors = array_unique( data.map(([_, sector]) =&gt; sector) );
const currencies = array_unique( data.map(([currency]) =&gt; currency) ).sort();

const sector_map = Object.fromEntries(
  sectors.map((sector, index) =&gt; [sector, index])
);

const currency_map = Object.fromEntries(
  currencies.map((currency, index) =&gt; [currency, index])
);

// zero fill 2 dimensional array
const values_matrix = sectors.map( () =&gt; currencies.map(() =&gt; 0) );

// increment values in the 2 dimensional array for each item in the data array
// getting the row index and column index from the sector_map and currency_map
for(const [currency, sector] of data) {
  values_matrix[ sector_map[sector] ][ currency_map[currency] ]++;
}


// create rows for a table
const rows = [[&#39;Sector&#39;, ...currencies]];
for(const [index, sector] of sectors.entries()) {
  rows.push([sector, ...values_matrix[index]]);
}

console.log(rows.map((row) =&gt; row.join(&#39;,&#39;)));

<!-- end snippet -->

答案2

得分: 0

尝试使用以下代码来格式化数据数组:

const data = [
  ['Euro', 'Tech'],
  ['USD', 'Tech'],
  ['GBX', 'Health'],
  ['Euro', 'Real Estate'],
  ['GBX', 'Real Estate'],
];

let res = {};
let currencies = [];
data.map((item) => {
  const [val, key] = item;
  res[key] ? res[key].push(val) : (res[key] = [val]);
  if (!currencies.includes(val)) currencies.push(val);
});

const rows = Object.keys(res); // Tech, Health, Real Estate

const table = new Array(rows.length);
let index = 0;

for (let row = 0; row < currencies.length; row++) {
  key = rows[row];
  const values = Object.values(res[key]);
  table[index] = { title: key, values: new Array(currencies.length) };
  for (let i = 0; i < currencies.length; i++) {
    const element = currencies[i];
    if (values.includes(element)) {
      table[index].values[i] = { title: element, value: 1 };
    } else {
      table[index].values[i] = { title: element, value: 0 };
    }
  }
  index++;
}

console.log(JSON.stringify(table, null, 2));

结果将类似于以下内容:

[
  {
    "title": "Tech",
    "values": [
      {
        "title": "Euro",
        "value": 1
      },
      {
        "title": "USD",
        "value": 1
      },
      {
        "title": "GBX",
        "value": 0
      }
    ]
  },
  {
    "title": "Health",
    "values": [
      {
        "title": "Euro",
        "value": 0
      },
      {
        "title": "USD",
        "value": 0
      },
      {
        "title": "GBX",
        "value": 1
      }
    ]
  },
  {
    "title": "Real Estate",
    "values": [
      {
        "title": "Euro",
        "value": 1
      },
      {
        "title": "USD",
        "value": 0
      },
      {
        "title": "GBX",
        "value": 1
      }
    ]
  }
]
英文:

try this code to format data array

const data = [
  [&#39;Euro&#39;, &#39;Tech&#39;],
  [&#39;USD&#39;, &#39;Tech&#39;],
  [&#39;GBX&#39;, &#39;Health&#39;],
  [&#39;Euro&#39;, &#39;Real Estate&#39;],
  [&#39;GBX&#39;, &#39;Real Estate&#39;],
];

let res = {};
let currencies = [];
data.map((item) =&gt; {
  const [val, key] = item;
  res[key] ? res[key].push(val) : (res[key] = [val]);
  if (!currencies.includes(val)) currencies.push(val);
});

const rows = Object.keys(res); //Tech, Health, Real Estate

const table = new Array(rows.length);
let index = 0;

for (let row = 0; row &lt; currencies.length; row++) {
  key = rows[row];
  const values = Object.values(res[key]);
  table[index] = { title: key, values: new Array(currencies.length) };
  for (let i = 0; i &lt; currencies.length; i++) {
    const element = currencies[i];
    if (values.includes(element)) {
      table[index].values[i] = { title: element, value: 1 };
    } else {
      table[index].values[i] = { title: element, value: 0 };
    }
  }
  index++;
}

console.log(JSON.stringify(table, null, 2));

result will be like this

[
  {
    &quot;title&quot;: &quot;Tech&quot;,
    &quot;values&quot;: [
      {
        &quot;title&quot;: &quot;Euro&quot;,
        &quot;value&quot;: 1
      },
      {
        &quot;title&quot;: &quot;USD&quot;,
        &quot;value&quot;: 1
      },
      {
        &quot;title&quot;: &quot;GBX&quot;,
        &quot;value&quot;: 0
      }
    ]
  },
  {
    &quot;title&quot;: &quot;Health&quot;,
    &quot;values&quot;: [
      {
        &quot;title&quot;: &quot;Euro&quot;,
        &quot;value&quot;: 0
      },
      {
        &quot;title&quot;: &quot;USD&quot;,
        &quot;value&quot;: 0
      },
      {
        &quot;title&quot;: &quot;GBX&quot;,
        &quot;value&quot;: 1
      }
    ]
  },
  {
    &quot;title&quot;: &quot;Real Estate&quot;,
    &quot;values&quot;: [
      {
        &quot;title&quot;: &quot;Euro&quot;,
        &quot;value&quot;: 1
      },
      {
        &quot;title&quot;: &quot;USD&quot;,
        &quot;value&quot;: 0
      },
      {
        &quot;title&quot;: &quot;GBX&quot;,
        &quot;value&quot;: 1
      }
    ]
  }
]

huangapple
  • 本文由 发表于 2023年2月14日 20:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447849.html
匿名

发表评论

匿名网友

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

确定