如何在javascript/typescript中基于另一个数组创建动态数组?

jbose2ul  于 2023-01-04  发布在  Java
关注(0)|答案(4)|浏览(129)

我想创建一个数组来循环第一个数组的一个参数(在本例中,所需的参数是DT),并检查在这些日期是否有不同应用程序的数据,如果有,它将把它的值放入第二个数组中,如果没有,它将放入0。
我所做的也是const pluck = (arr, key) => arr.map(i => i[key]);,我获得了所需的字段日期(但它们有重复的值)。为了删除重复的值,我使用了dates = [...new Set(dates)];,最后循环了最终的值并编写了一系列代码,但我没有得到我想要的(下面的预期数组)。

first_array = [
  {
    DT: "2022-01-01",
    APP: "Application 1",
    SPEED: 1547,
  },
  {
    DT: "2022-01-01",
    APP: "Application 2",
    SPEED: 685,
  },
  {
    DT: "2022-01-02",
    APP: "Application 1",
    SPEED: 500,
  },
  {
    DT: "2022-01-02",
    APP: "Application 2",
    SPEED: 300,
  },
  {
    DT: "2022-01-02",
    APP: "Application 3",
    SPEED: 600,
  },
  {
    DT: "2022-01-03",
    APP: "Application 1",
    SPEED: 1000,
  },
]

需要数组

desire_array = [
  {
    Name: "Application1",
    Values: [1547, 500, 1000],
    ValuesWithDate: [{x: '2022-01-01', y: 1547}, {x: '2022-01-02', y: 500}, {x: '2022-01-03', y: 1000}],
  },
  {
    Name: "Application2",
    Values: [685, 300, 0],
    ValuesWithDate: [{x: '2022-01-01', y: 685}, {x: '2022-01-02', y: 300}, {x: '2022-01-03', y: 0}],
  },
  {
    Name: "Application3",
    Values: [0, 600, 0],
    ValuesWithDate: [{x: '2022-01-01', y: 0}, {x: '2022-01-02', y: 600}, {x: '2022-01-03', y: 0}],
  },
]

我需要这样做的原因是创建一个series,我可以使用它来显示ApexCharts图表。
真实的数据也可以从this api显示为JSON。

gkl3eglg

gkl3eglg1#

您可以:

const first = [{DT: '2022-01-01',APP: 'Application 1',SPEED: 1547,},{DT: '2022-01-01',APP: 'Application 2',SPEED: 685,},{DT: '2022-01-02',APP: 'Application 1',SPEED: 500,},{DT: '2022-01-02',APP: 'Application 2',SPEED: 300,},{DT: '2022-01-02',APP: 'Application 3',SPEED: 600,},{DT: '2022-01-03',APP: 'Application 1',SPEED: 1000,}]

const dates = [...new Set(first.map(({ DT }) => DT))]
const apps = [...new Set(first.map(({ APP }) => APP))]

const result = apps.reduce((acc, app) => {
  const appData = Object.assign(
    {},
    {
      Name: app.replace(/ /, ''),
      Values: [],
      ValuesWithDate: [],
    }
  )

  dates.forEach((date) => {
    const data = first.find(({ DT, APP }) => DT === date && APP === app)
    appData.ValuesWithDate.push({ x: date, y: data ? data.SPEED : 0 })
    appData.Values.push(data ? data.SPEED : 0)
  })

  acc.push(appData)
  return acc
}, [])

console.log(result)
o7jaxewo

o7jaxewo2#

你可以试试这样的东西:

const convertArray = (arr) => arr.reduce((prev, current) => {
    const existingIndex = prev.findIndex((p) => p.Name === current.APP);
  if (existingIndex > -1) {
    const currentApp = prev[existingIndex];
    currentApp.Values.push(current.SPEED);
    currentApp.ValuesWithDate.push({x: current.DT, y: current.SPEED});
    prev[existingIndex] = currentApp;
  } else {
    prev.push({Name: current.APP, Values: [current.SPEED], ValuesWithDate:[{x: current.DT, y: current.SPEED}]})
  }
    return prev;
}, []);

并像这样使用它:

const desire_array = convertArray(first_array)
vngu2lb8

vngu2lb83#

const convert = (dates, data) => {
    return Object.values(data.reduce((acc, curr) => {
        if (!acc[curr.APP]) {
            acc[curr.APP] = {
                name: curr.APP,
                valuesWithDate: []
            };
        }
        acc[curr.APP].valuesWithDate.push({
            x: curr.DT,
            y: curr.SPEED
        });
        return acc;
    }, {})).map((dataWithoutGaps) => {
        const valuesWithDate = [...new Set(dates)].map(date => {
            const el = dataWithoutGaps.valuesWithDate.find(e => e.x === date);
            return {
                x: date,
                y: el ? el.y : 0
            };
        });
        return {
            ValuesWithDate: valuesWithDate,
            Values: valuesWithDate.map(e => e.y),
            Name: dataWithoutGaps.name
        }
    });
};


console.log(convert(first_array.map(e => e.DT), first_array));

预期:

[{"ValuesWithDate":[{"x":"2022-01-01","y":1547},{"x":"2022-01-02","y":500},{"x":"2022-01-03","y":1000}],"Values":[1547,500,1000],"Name":"Application 1"},{"ValuesWithDate":[{"x":"2022-01-01","y":685},{"x":"2022-01-02","y":300},{"x":"2022-01-03","y":0}],"Values":[685,300,0],"Name":"Application 2"},{"ValuesWithDate":[{"x":"2022-01-01","y":0},{"x":"2022-01-02","y":600},{"x":"2022-01-03","y":0}],"Values":[0,600,0],"Name":"Application 3"}]
b5buobof

b5buobof4#

您期望的结果可以得到这个代码。

let filtered_app = new Set();
const obj = [];

first_array.forEach(item=>{
  filtered_app.add(item.APP);
});
filtered_app.forEach(app =>{
  first_array.forEach(item =>{
    if(item.APP == app){
      const exists = obj.findIndex(elem => elem.Name == app);
      if(exists != '-1'){
        obj[exists].Values.push(item.SPEED);
        obj[exists].ValuesWithDate.push({x: item.DT, y: item.SPEED});
      }
      else{
        obj.push({Name: app, Values: [item.SPEED], ValuesWithDate: [{x: item.DT, y: item.SPEED}]});
      }
    }
  });
});
console.log(obj);

希望有帮助。

相关问题