如何遍历对象和数组并创建新的对象javascript [英] How to loop through object and arrays and create new object javascript

查看:704
本文介绍了如何遍历对象和数组并创建新的对象javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在javascript
中将对象更改为对象的新数组如何循环遍历对象和数组并创建新的对象javascript。
我有对象 obj

I would like to know to how to change the object to new array of object in javascript How to loop through object and arrays and create new object javascript. I have object obj,

group 键代表周(开始-结束)

group key represents the weeks (start-end),

数组中,应创建一个每个
的数组列表

in columns array, should create a arraylist of each

项目描述为value,desc为col,

item description as value and desc as col,

intervals(begin-end)为col,value为数量
finalqty键为col,value为finalqty
finalamt键作为col,值作为finalam值
(即每周)

intervals(begin-end) as col and value as qty finalqty key as col and value as finalqty value finalamt key as col and value as finalamt value (ie)per week

function newObj(obj){
 var result = obj.products.map(e=>({
     group: Object.values(obj.options).map((opt, index) =>opt.start+"-"+opt.end),
     columns: [{
       col: "desc",
      value: e.desc}
    ]

  }))
}


const obj =  {
  options: {
    w1: {start:"Jan",end: "1"},
    w2: {start:"Feb", end: "1"}
  },
  intervals: {
    t1: {begin: "1", end: "2", totalqty: 2,totalamt: 200},
    t2: {begin: "4", end: "7", totalqty: 3, totalamt: 300}
  },
  items: [
    {
      name: "s1",
      desc: "sample1",
      w1: {t1: {qty:0},t2: {qty:1},finalqty:4,finalamt:300},
      w2: {t1: {qty:1},t2: {qty:2},finalqty:6,finalamt:400}
    },
    {
      name: "s2",
      desc: "sample2",
      w1: {t1: {qty:0},t2: {qty:0}, finalqty:5,finalamt:100},
      w2: {t1: {qty:0},t2: {qty:1}, finalqty:8,finalamt:70}}
    }
  ]
}


预期产量

[
[
  {
    group:"Jan 1", // represents w1
    columns: [
      {
      col: 'desc',
      value: 'sample1' // item.desc
      },    
      {
       col: '1-2', // represents t1
       value: 0   , // represents t1.qty
      },
      {
       col: '4-7', // represents t2   
       value: 1 // represents w1.t2.qty   
      } ,{
       col: "finalqty",
       value: 4
      },
      {
       col: "finalamt",
       value: 300
      }
    ]
  },
  {
    group:"Feb 1", // represents w2
    columns: [
      {
      col: 'desc',
      value:'sample1' 
      },
      {
      col: '1-2', // represents t1
        value:1   , // represents t1.qty
      },
      {
       col: '4-7', // represents t2
        value:2 ,// represents t2.qty
      },
      ,{
       col: "finalqty",
       value: 6
      },
      {
       col: "finalamt",
       value: 400
      }
   ]
  },
  {
   group:"Jan 1", 
    columns: [
      {
      col: 'desc',
       value:'sample2' 
      },    
      {
      col: '1-2', 
        value:0, 
      },
      {
       col: '4-7', 
        value:0 
      } 
      ,{
       col: "finalqty",
       value: 5
      },
      {
       col: "finalamt",
       value: 100
      }
    ]
  },
  {
   group:"Feb 1",
    columns: [
      {
     col: 'desc',
       value:'sample2' 
      },
      {
       col: '1-2',
        value:0   ,
      },
      {
       col: '4-7',
        value:1,
      },
      {
       col: "finalqty",
       value: 8
      },
      {
       col: "finalamt",
       value: 70
      }
   ]
  }
]


推荐答案

我想解释方法/算法以解决该问题。这样可以帮助您将来解决类似的问题

I would like to explain the approach/algorithm to solve the problem. So that it might help you to solve the similar problems in future

// Declare an empty array
const results = [];

/* Since you need result as input object weeks x 2 times (4 objects in output array)
 * We need to perform outer loop with the obj.items
 * Inner loops with interval/options combination
 * Push the final value to results
*/
for (... of obj.items) { // 2 iterations
  // Declare an array with data needed from items
  const columns = [...];
  // Collect the data from items in object
  for (... in obj.intervals) {
    // Collect the data from interval and add it to columns
  }
  for (... in obj.options) { // 2 iterations
    // Collect data from options
    const output = {...};
    // Add columns collected above to output
    // Push the final output to results
    results.push(output); // 4 objects in output at the end
  }
}




< h3>使用上述方法更新了实际的代码答案(根据要求几乎没有即兴


Updated the actual code answer with above approach (With little improvisation as per requirement)

const obj =  {
  options: {
    w1: {start:"Jan",end: "1"},
    w2: {start:"Feb", end: "1"}
  },
  intervals: {
    t1: {begin: "1", end: "2", totalqty: 2, totalamt: 200},
    t2: {begin: "4", end: "7", totalqty: 3, totalamt: 300}
  },
  items: [
    {
      name: "s1",
      desc: "sample1",
      w1: {t1: {qty:0},t2: {qty:1}, finalqty:4, finalamt:300},
      w2: {t1: {qty:1},t2: {qty:2}, finalqty:6, finalamt:400}
    },
    {
      name: "s2",
      desc: "sample2",
      w1: {t1: {qty:0},t2: {qty:0}, finalqty:5, finalamt:100},
      w2: {t1: {qty:0},t2: {qty:1}, finalqty:8, finalamt:70}
    }
  ]
}

const results = [];

for(let item of obj.items) {
  for(let opt in obj.options) {
    const columns = [{col: 'desc', value: item.desc}];

    for (let key in obj.intervals) {
      columns.push({
        col: `${obj.intervals[key].begin}-${obj.intervals[key].end}`,
        value: item[opt][key].qty
      });
    }

    results.push({
      group: `${obj.options[opt].start} ${obj.options[opt].end}`,
      columns: columns.concat([{
        col: 'finalqty',
        value: item[opt].finalqty
      },
      {
        col: 'finalamt',
        value: item[opt].finalamt
      }])
    });
  }
}

console.log(results);

这篇关于如何遍历对象和数组并创建新的对象javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆