Untitled conversation
- User
[ { "type": 2, "list": [ { "id": 0, "type": 2, "prop": "填充颜色", "from": { "hex": "FF2222", "cmyk": "0,5,0,5" }, "to": { "hex": "", "cmyk": "0,10,50,2" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色2", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色3", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色4", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色5", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色6", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" } ] }, { "type": 4, "list": [ { "id": 0, "type": 4, "prop": "边框颜色", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色2", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色3", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色4", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色5", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色6", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" } ] } ]
将数组 转为为【】
Assistant
Okay, you want to flatten the array of objects, taking all the items from the nested
listarrays and putting them into a single top-level array.Here's how you can do it in JavaScript:
const inputArray = [ { "type": 2, "list": [ { "id": 0, "type": 2, "prop": "填充颜色", "from": { "hex": "FF2222", "cmyk": "0,5,0,5" }, "to": { "hex": "", "cmyk": "0,10,50,2" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色2", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色3", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色4", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色5", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色6", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" } ] }, { "type": 4, "list": [ { "id": 0, "type": 4, "prop": "边框颜色", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色2", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色3", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色4", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色5", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色6", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" } ] } ]; // Using flatMap (ES2019+) - most concise const flattenedArray = inputArray.flatMap(item => item.list); // Alternative using reduce (works in older environments) // const flattenedArray = inputArray.reduce((acc, current) => { // return acc.concat(current.list); // }, []); // Alternative using a for...of loop and spread syntax // const flattenedArray = []; // for (const item of inputArray) { // if (item.list && Array.isArray(item.list)) { // defensive check // flattenedArray.push(...item.list); // } // } console.log(JSON.stringify(flattenedArray, null, 2));Explanation:
inputArray.flatMap(item => item.list):flatMap()is a JavaScript array method that first maps each element using a mapping function, then flattens the result into a new array. It's equivalent to amap()followed byflat(1).item => item.list: This arrow function is the mapping function. For eachitem(object) ininputArray, it returnsitem.list(which is the nested array you want to extract).flatMapthen takes all these returneditem.listarrays and concatenates them into a single new array.
The output will be:
[ { "id": 0, "type": 2, "prop": "填充颜色", "from": { "hex": "FF2222", "cmyk": "0,5,0,5" }, "to": { "hex": "", "cmyk": "0,10,50,2" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色2", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色3", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色4", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色5", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 2, "prop": "填充颜色6", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色2", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色3", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色4", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色5", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" }, { "id": 0, "type": 4, "prop": "边框颜色6", "from": { "hex": "", "cmyk": "" }, "to": { "hex": "", "cmyk": "" }, "remark": "" } ]
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.