我想知道如何在javascript中将对象更改为新的对象数组
如何遍历对象和数组并创建新的对象javascript。
我有对象obj
,
group
键代表星期(开始-结束)
columns
数组中的中,应创建以下内容的数组列表 每个
项目描述为值,desc为col,
intervals(begin-end)为col,值为qty finalqty键作为col,值作为finalqty值 finalamt键为col,值为finalamt值 (ie)每周
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
}
]
}
]
答案 0 :(得分:2)
要遍历对象和数组,请使用“ for”循环。
Oject = for / in;数组= for / of
这说明了这一点:https://www.w3schools.com/js/js_loop_for.asp
对于问题的第一部分(“我想知道如何在javascript中将对象更改为新的对象数组”)
答案 1 :(得分:1)
我想说明方法/算法以解决该问题。这样可以帮助您将来解决类似的问题
// 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
}
}
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);