【问题标题】:Format array of objects格式化对象数组
【发布时间】:2022-01-14 14:39:40
【问题描述】:

我有这个对象(数据)数组。这些是第一个索引:

0: Object

granularity_time: "total"

granularity_geo: "nation"

location_code: "norge"

border: "2020"

age: "90+"

sex: "female"

year: "1900"

week: "1"

yrwk: "1900-01"

season: "1899/1900"

x: "24"

date: "1900-01-01"

n: "219"

date_of_publishing: "2022-01-12"

    tag_outcome: "death"

1: Object

granularity_time: "total"

granularity_geo: "nation"

location_code: "norge"

border: "2020"

age: "90+"

sex: "male"

year: "1900"

week: "1"

yrwk: "1900-01"

season: "1899/1900"

x: "24"

date: "1900-01-01"

n: "127"

date_of_publishing: "2022-01-12"

tag_outcome: "death"

它的统计,男女同龄有自己的对象。索引 0 代表 90 岁以上的女性,索引 1 代表 90 岁以上的男性。索引 2 适用于女性 80+,索引 3 男性 80+ 等等。

我想对数据进行格式化,以便每个索引包含两个对象,用于同一年龄的男性和女性。

类似这样的:

const newData = [
{
  woman: data[0],
  men: data[1]
},
{
  woman: data[2],
  men: data[3]
},
{
  woman: data[4],
  men: data[5]
},
{
  woman: data[6],
  men: data[7]
},
{
  woman: data[8],
  men: data[9]
},
{
  woman: data[10],
  men: data[11]
},
{
  woman: data[12],
  men: data[13]
}

];

我试图创建一个迭代数据的函数,但每个条目最终都未定义:

const formatData = (data) => {
    const newData = [];

    data?.map((item, i) => {
      i % 2 === 0 ? newData.push({ woman: item[i], men: item[i++] }) : null;
    });

    return newData;
  };

我在这里错过了什么?

【问题讨论】:

  • “我在这里缺少什么?” - 首先,如何为您的问题提供一个正确的 minimal reproducible example。请举例说明您希望哪些确切的输入数据导致显示的结果,而不仅仅是一个仍然很模棱两可的模糊口头描述。
  • 我想你可能误解了map回调中的item实际上是...?

标签: javascript arrays object


【解决方案1】:

由于输出是输入的一半,它实际上更像是一个 reduce()...

const data = [{ gender: "m", name: "sam" }, { gender: "f", name: "sally" }, { gender: "m", name: "allen" }, { gender: "f", name: "abby" }, { gender: "m", name: "jim" }, { gender: "f", name: "jill" }];

const newData = data.reduce((acc, obj, i) => {
  i % 2 ? acc[acc.length-1].woman = obj : acc.push({ man: obj })
  return acc;
}, []);

console.log(newData)

【讨论】:

    【解决方案2】:

    您可以使用 Array.from() 和 Array.map() 从原始数据数组创建所需的输出。

    新数组的长度将为 data.length / 2,每个备用项将分配给输出数组中每个元素的 men 或 women 属性。

    const template = { year: 1900, location_code: 'norge' };
    // Some array of input data...
    const data = Array.from({length: 14}, (v,k) => ({...template, id: k, gender: ( k % 2 === 0 ? 'female':'male' )}));
    
    const result = Array.from( { length: data.length / 2 }, (v, idx) => { 
        return { women: data[2*idx], men: data[2*idx + 1],  };
    })
    
    console.log(result)
        
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      这确实有效,但我不确定它是否是最佳实践,因为我只是使用地图来获取 i..

      const formatData = (data) => {
      const newData = [];
      data?.map((item, i) => {
        i % 2 === 0
          ? newData.push({ woman: data[i], men: data[i + 1] })
          : null;
      });
      return newData;
      

      };

      【讨论】:

      • 这没有什么坏处,但是你说得对,让 map 产生副作用并忽略它的返回值就像不使用 map 一样。如果你想使用函数式风格,那么只有 reduce 才有意义;否则,传统的 for 循环在风格上是最清晰的。
      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      相关资源
      最近更新 更多