数据处理

let data = [
    {id:1,value:'a'},
    {id:1,value:'ab'},
    {id:1,value:'abc'},
    {id:1,value:'abcd'},
    {id:2,value:'e'},
    {id:2,value:'ef'},
    {id:3,value:'g'},
    {id:3,value:'gh'},
    {id:4,value:'i'},
    {id:5,value:'j'},
    {id:6,value:'k'},
    {id:6,value:'kl'},
    {id:7,value:'m'},
    {id:8,value:'n'},
]
let arr=[]
let temp=[]
data.push({})
data.reduce((prev,next,index,ary)=>{
    temp.push(prev)
    if(prev.id != next.id){
      arr.push(temp)
      temp = []
    }
    return next
})

console.log('arr',arr)

 

let ary11 = [
{item_code: '总NOx', the_month: '2025-01', curr_month_value: 24.53},
{item_code: '总SO2', the_month: '2025-01', curr_month_value: 11.63},
{item_code: '总烟尘', the_month: '2025-01', curr_month_value: 1.5},
{item_code: '总NOx', the_month: '2025-02', curr_month_value: 19.74},
{item_code: '总SO2', the_month: '2025-02', curr_month_value: 11.08},
{item_code: '总烟尘', the_month: '2025-02', curr_month_value: 1.44},
{item_code: '总NOx', the_month: '2025-03', curr_month_value: 16.51},
{item_code: '总SO2', the_month: '2025-03', curr_month_value: 8.76},
{item_code: '总烟尘', the_month: '2025-03', curr_month_value: 1.23},
{item_code: '总NOx', the_month: '2025-04', curr_month_value: 30.36}, 
{item_code: '总SO2', the_month: '2025-04', curr_month_value: 19.43},
{item_code: '总烟尘', the_month: '2025-04', curr_month_value: 3.21},
]
//获取月份
const xData = [...new Set(sortedData.map(item => item.the_month))].sort();
//处理数据
const result1 = ary11.reduce((acc, curr) => {
  const index = ['总NOx', '总SO2', '总烟尘'].indexOf(curr.item_code);
  if (!acc[index]) acc[index] = [];
  acc[index].push(curr.curr_month_value);
  return acc;
}, []);

console.log(result1);
// [24.53, 19.74, 16.51, 30.36]
// [11.63, 11.08, 8.76, 19.43]
// [1.5, 1.44, 1.23, 3.21]
//方法2
const result2 = [];
const itemMap = {};

ary11.forEach(item => {
  if (!itemMap[item.item_code]) {
    itemMap[item.item_code] = [];
    result2.push(itemMap[item.item_code]);
  }
  itemMap[item.item_code].push(item.curr_month_value);
});

console.log(result2);

let arr=[]
let temp=[]
sortedData.push({})
sortedData.reduce((prev,next,index,ary)=>{
  temp.push(prev)
  if(prev.the_month != next.the_month){
    arr.push(temp)
    temp = []
  }
  return next
})
console.log('arr',arr)
[[
  {item_code: '总NOx', the_month: '2025-01', curr_month_value: 24.53},
  {item_code: '总SO2', the_month: '2025-01', curr_month_value: 11.63},
  {item_code: '总烟尘', the_month: '2025-01', curr_month_value: 1.5}
],
[
  {item_code: '总NOx', the_month: '2025-02', curr_month_value: 19.74},
  {item_code: '总SO2', the_month: '2025-02', curr_month_value: 11.08},
  {item_code: '总烟尘', the_month: '2025-02', curr_month_value: 1.44}
],
[
  {item_code: '总NOx', the_month: '2025-03', curr_month_value: 16.51},
  {item_code: '总SO2', the_month: '2025-03', curr_month_value: 8.76},
  {item_code: '总烟尘', the_month: '2025-03', curr_month_value: 1.23}
],
[
  {item_code: '总NOx', the_month: '2025-04', curr_month_value: 30.36},
  {item_code: '总SO2', the_month: '2025-04', curr_month_value: 19.43},
  {item_code: '总烟尘', the_month: '2025-04', curr_month_value: 3.21}
]]
const xData = arr.map(item => item[0].the_month);
let nox = []
let so2 = []
let yc = []
arr.forEach(item=>{
  item.forEach(it=>{
    if(it.item_code == '总NOx'){
      nox.push(it.curr_month_value)
    }else if(it.item_code == '总SO2'){
      so2.push(it.curr_month_value)
    }else if(it.item_code == '总烟尘'){
      yc.push(it.curr_month_value)
    }
  })
})
let series = []
series.push(nox)
series.push(so2)
series.push(yc)

 

const data = {"bitcoin":"76002.57","ethereum":"2256.30"};转为[{name:'bitcoin',value:'76002.57'},{name:'ethereum',value:'2256.30'}]
const result1 = Object.entries(data).map(([name, value]) => ({
  name,
  value
}));
const result2 = Object.keys(data).map(key => ({
  name: key,
  value: data[key]
}));
const result3 = Object.entries(data).reduce((acc, [name, value]) => {
  acc.push({ name, value });
  return acc;
}, []);

 

let typeAry = [
        {type:'1',name:'风光摄影'},
        {type:'2',name:'肖像摄影'},
        {type:'3',name:'婚纱摄影'},
        {type:'4',name:'儿童摄影'}
    ]
let curData1 = [
        {
            type:'1,2,3',
            typeN:'',
        },
        {
            type:'2,3',
            typeN:'',
        }
    ]
const map = Object.fromEntries(typeAry.map(t => [t.type, t.name]));
curData1.forEach(item => item.typeN = item.type.split(',').map(id => map[id] || id).join(','));

const typeMap = {};
typeAry.forEach(item => {
  typeMap[item.type] = item.name;
});
// 处理 curData1
curData1 = curData1.map(item => {
  // 将 type 字符串按逗号分割成数组
  const typeIds = item.type.split(',');
  // 将每个 id 映射为对应的 name
  const typeNames = typeIds.map(id => typeMap[id] || id); // 如果找不到映射,保留原 id
  // 将 name 数组用逗号连接
  item.typeN = typeNames.join(',');
  return item;
});
console.log(curData1);

 

posted @ 2026-01-04 14:01  峪野  阅读(10)  评论(0)    收藏  举报