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