【问题标题】:Need help getting the value of object length in array需要帮助获取数组中对象长度的值
【发布时间】:2021-02-25 21:17:05
【问题描述】:

我有一个看起来像这样的数组。

0: Object { age: 1.22128, initials: "YY", major: "Internet Content & Information" }
​
1: Object { age: 0.623491, initials: "RAM", major: "Banks Diversified" }
​
2: Object { age: 3.47226, initials: "W", major: "Internet Retail" }
​
3: Object { age: 0.10981, initials: "DAM", major: "Communication Equipment" }
​
4: Object { age: 0, symbol: initials", major: "Software Application" }
​
5: Object { age: 0.82384, initials: "SAM", major: "Internet Content & Information" }
​
6: Object { age: 1.62574, initials: "LISA", major: "Internet Content & Information" }
​
7: Object { age: 0, initials: "AN", college: "major Content & Information" }
​
8: Object { age: 1.34786, initials: "PAM", major: "REIT Retail" }
​
9: Object { age: 0, initials: "DREW", major: "Software—Infrastructure" }

我想做的是把专业分类成一个图表,我可以使用这段代码来做到这一点。下面。

      var data = array, 
  result = _(data)
  .groupBy('major')
  .map((group, major) => 
      ({ industry, children: _.map(group, ({ initials: major, age }) => ({ major, age })) }))
  .value(),
// final = [{ name: "Children Array", children: result }];
final = result

我用来将首字母分配给专业的图表采用这样的值。

  const data = [
  {
    type: 'Retail',
    value: 38,
  },
  {
    type: 'Software',
    value: 5,
  },
  {
    type: 'Engineering',
    value: 35,
  },
  {
    type: 'Real Estate',
    value: 15,
  },
  {
    type: 'HealthCare',
    value: 7,
  },
] 

使用上面的代码摘录,我能够将它放在一个看起来像这样的数组中。

(18) […]
​
0: Object { major: "Internet Content & Information", children: (5) […] }
​
1: Object { major: "Banks Diversified", children: (1) […] }
​
2: Object { major: "Internet Retail", children: (1) […] }
​
3: Object { major: "Communication Equipment", children: (2) […] }
​
4: Object { major: "Software Application", children: (3) […] }

孩子们指出该专业中有多少个首字母,有没有办法可以创建一个新数组来模仿图表在代码中的方式,这样我就可以将专业作为类型,将孩子的值作为值在图表中。

如果有人可以帮助我将顶部的数组转换为图表的数据语法,我也会很高兴。 谢谢你

【问题讨论】:

    标签: javascript node.js arrays json object


    【解决方案1】:

    您可以首先创建带有键的对象作为所有主要元素,然后循环通过它来创建您想要的数组 见下sn-p

    const input = [
      { age: 1.22128, initials: "YY", major: "Internet Content & Information" },
      { age: 0.623491, initials: "RAM", major: "Banks Diversified" },
      { age: 3.47226, initials: "W", major: "Internet Retail" },
      { age: 0.10981, initials: "DAM", major: "Communication Equipment" },
      { age: 0, initials: "SHA", major: "Software Application" },
      { age: 0.82384, initials: "SAM", major: "Internet Content & Information" },
      { age: 1.62574, initials: "LISA", major: "Internet Content & Information" },
      { age: 0, initials: "AN", major: "major Content & Information" },
      { age: 1.34786, initials: "PAM", major: "REIT Retail" },
      { age: 0, initials: "DREW", major: "Software—Infrastructure" },
    ];
    
    const obj = {};
    input.forEach((row) => {
      if (!obj[row.major]) obj[row.major] = 1;
      obj[row.major]++;
    });
    const output = [];
    for (var key in obj) {
      output.push({ type: key, value: obj[key] });
    }
    console.log(output);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 2015-07-16
      • 1970-01-01
      • 2016-12-11
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多