简体   繁体   English

如何循环通过 object 和 arrays 并创建新的 object javascript

[英]How to loop through object and arrays and create new object javascript

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 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 ,我有 object obj

group key represents the weeks (start-end), group键代表周(开始-结束),

in columns array, should create a arraylist of eachcolumns数组中,应该创建一个 arraylist 的每个

item description as value and desc as col,项目描述为值,描述为列,

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间隔(开始-结束)作为 col 和 value 作为 qty finalqty 键作为 col 和 value 作为 finalqty 值 finalamt 键作为 col 和 value 作为 finalamt 值(即)每周

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}}
    }
  ]
}


Expected Output预计 Output

[
[
  {
    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
  }
}

Updated the actual code answer with above approach ( With little improvisation as per requirement )用上述方法更新了实际的代码答案( 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);

To loop through objects and arrays use 'for' loop.要遍历对象和 arrays 使用“for”循环。

Oject = for/in;对象 = for/in; Array = for/of数组 = for/of

This explains it: https://www.w3schools.com/js/js_loop_for.asp这解释了它: https://www.w3schools.com/js/js_loop_for.asp

For the first part of the question ("I would like to know to how to change the object to new array of object in javascript") could you please be more precise?对于问题的第一部分(“我想知道如何在 javascript 中将 object 更改为 object 的新数组”)你能更准确一点吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM