如何遍历对象和数组并创建新对象 javascript
How to loop through object and arrays and create new object javascript
我想知道如何在 javascript 中将对象更改为新的对象数组
如何遍历对象和数组并创建新对象 javascript。
我有对象 obj
,
group
键代表星期(开始-结束),
在 columns
数组中,应该创建一个数组列表
每个
项目描述为值,描述为列,
intervals(begin-end) 作为列,值作为数量
finalqty 键作为 col,值作为 finalqty 值
finalamt 键作为 col,值作为 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}}
}
]
}
预期输出
[
[
{
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
}
]
}
]
要遍历对象和数组,请使用 'for' 循环。
对象 = for/in ;数组 = for/of
这解释了它:https://www.w3schools.com/js/js_loop_for.asp
对于问题的第一部分(“我想知道如何将 javascript 中的对象更改为新的对象数组”)能否请您更准确些?
我想解释一下approach/algorithm解决问题。方便以后解决类似问题
// 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);
我想知道如何在 javascript 中将对象更改为新的对象数组
如何遍历对象和数组并创建新对象 javascript。
我有对象 obj
,
group
键代表星期(开始-结束),
在 columns
数组中,应该创建一个数组列表
每个
项目描述为值,描述为列,
intervals(begin-end) 作为列,值作为数量 finalqty 键作为 col,值作为 finalqty 值 finalamt 键作为 col,值作为 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}}
}
]
}
预期输出
[
[
{
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
}
]
}
]
要遍历对象和数组,请使用 'for' 循环。
对象 = for/in ;数组 = for/of
这解释了它:https://www.w3schools.com/js/js_loop_for.asp
对于问题的第一部分(“我想知道如何将 javascript 中的对象更改为新的对象数组”)能否请您更准确些?
我想解释一下approach/algorithm解决问题。方便以后解决类似问题
// 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);