Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

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,

group key represents the weeks (start-end),

in columns array, should create a arraylist of each

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

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

[
[
  {
    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
      }
   ]
  }
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...