Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 136 characters in body
Source Link
jsejcksn
  • 34.6k
  • 5
  • 55
  • 79

Iterate the outer array,destructuring its properties, then — indo the same for each inner array iteration — push, pushing the data you want in your result:

const input = [
 {
 MC: "11233",
 jobid: 113331,
 list: [
 { Q1: 1113, Q2: 333, code: "thisis1" },
 { Q1: 333, Q2: 111, code: "thisis2" },
 { Q1: 333, code: "thisis3" },
 ],
 },
 {
 MC: "332211",
 jobid: 3333,
 list: [
 { Q1: 444, Q2: 555, code: "thisis4" },
 ],
 },
];
const result = [];
for (const {MC, jobid, list} of input) {
 for (const {Q1, Q2, code} of list) {
 result.push(({MC, jobid, Q1, Q2, code}));
 }
}
console.log(result);

Iterate the outer array, then — in each inner array iteration — push the data you want in your result:

const input = [
 {
 MC: "11233",
 jobid: 113331,
 list: [
 { Q1: 1113, Q2: 333, code: "thisis1" },
 { Q1: 333, Q2: 111, code: "thisis2" },
 { Q1: 333, code: "thisis3" },
 ],
 },
 {
 MC: "332211",
 jobid: 3333,
 list: [
 { Q1: 444, Q2: 555, code: "thisis4" },
 ],
 },
];
const result = [];
for (const {MC, jobid, list} of input) {
 for (const {Q1, Q2, code} of list) {
 result.push(({MC, jobid, Q1, Q2, code}));
 }
}
console.log(result);

Iterate the outer array,destructuring its properties, then do the same for each inner array, pushing the data you want in your result:

const input = [
 {
 MC: "11233",
 jobid: 113331,
 list: [
 { Q1: 1113, Q2: 333, code: "thisis1" },
 { Q1: 333, Q2: 111, code: "thisis2" },
 { Q1: 333, code: "thisis3" },
 ],
 },
 {
 MC: "332211",
 jobid: 3333,
 list: [
 { Q1: 444, Q2: 555, code: "thisis4" },
 ],
 },
];
const result = [];
for (const {MC, jobid, list} of input) {
 for (const {Q1, Q2, code} of list) {
 result.push(({MC, jobid, Q1, Q2, code}));
 }
}
console.log(result);

Source Link
jsejcksn
  • 34.6k
  • 5
  • 55
  • 79

Iterate the outer array, then — in each inner array iteration — push the data you want in your result:

const input = [
 {
 MC: "11233",
 jobid: 113331,
 list: [
 { Q1: 1113, Q2: 333, code: "thisis1" },
 { Q1: 333, Q2: 111, code: "thisis2" },
 { Q1: 333, code: "thisis3" },
 ],
 },
 {
 MC: "332211",
 jobid: 3333,
 list: [
 { Q1: 444, Q2: 555, code: "thisis4" },
 ],
 },
];
const result = [];
for (const {MC, jobid, list} of input) {
 for (const {Q1, Q2, code} of list) {
 result.push(({MC, jobid, Q1, Q2, code}));
 }
}
console.log(result);

lang-js

AltStyle によって変換されたページ (->オリジナル) /