我如何从对象本身存储在对象数组中的对象(例如下面)中存储对象?

时间:2020-01-30 19:10:43

标签: javascript node.js

我是JavaScript新手。
我想从嵌套对象创建一个新数组。
我试图说明问题的结构。 我想将对象从数组的每个对象的子级中带到一个新的数组中。 提供:

   [
  {id: 10939, reference_id: 1, name: "A", value: "A", children: {
    1002: {id: 22, reference_id: 22, name: "A!", value: "A1"},
    1013: {id: 23, reference_id: 23, name: "A2", value: "A2"}
  }},
  {id: 10940, reference_id: 2, name: "B", value: "B", children: {
    1014: {id: 33, reference_id: 33, name: "B1", value: "B1"}
  }},
  {id: 10941, reference_id: 3, name: "C", value: "C", children: {
    IN01: {id: 44, reference_id: 44, name: "C1", value: "C1"},
    IN02: {id: 41, reference_id: 42, name: "C2", value: "C2"},
    IN03: {id: 55, reference_id: 22, name: "A!", value: "A1"},
    IN04: {id: 55, reference_id: 55, name: "G2", value: "G2"}
  }}
];

预期:

[{id: 22, reference_id: 22, name: "A!", value: "A1"},
 {id: 23, reference_id: 23, name: "A2", value: "A2"},
 {id: 33, reference_id: 33, name: "B1", value: "B1"},
 {id: 44, reference_id: 44, name: "C1", value: "C1"},
 {id: 41, reference_id: 42, name: "C2", value: "C2"},
 {id: 55, reference_id: 22, name: "A!", value: "A1"},
 {id: 55, reference_id: 55, name: "G2", value: "G2"}
]

怎么办?

2 个答案:

答案 0 :(得分:1)

.flatMap()将遍历您的Array,并使用函数的返回值形成一个新的Array,展平最终的Array

使用destructuring仅选择children项,然后使用Object.values仅返回那些项的值

const data = [
  {id: 10939, reference_id: 1, name: "A", value: "A", children: {
    1002: {id: 22, reference_id: 22, name: "A!", value: "A1"},
    1013: {id: 23, reference_id: 23, name: "A2", value: "A2"}
  }},
  {id: 10940, reference_id: 2, name: "B", value: "B", children: {
    1014: {id: 33, reference_id: 33, name: "B1", value: "B1"}
  }},
  {id: 10941, reference_id: 3, name: "C", value: "C", children: {
    IN01: {id: 44, reference_id: 44, name: "C1", value: "C1"},
    IN02: {id: 41, reference_id: 42, name: "C2", value: "C2"},
    IN03: {id: 55, reference_id: 22, name: "A!", value: "A1"},
    IN04: {id: 55, reference_id: 55, name: "G2", value: "G2"}
  }}
];

const resultFlat = data.flatMap(({children}) => Object.values(children));

console.log(resultFlat);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

请使用此:

## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# Threshold after which files are written to disk.
spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=200MB
# Max Request Size
spring.servlet.multipart.max-request-size=215MB

## File Storage Properties
# All files uploaded through the REST API will be stored in this directory
file.upload-dir=C:/Projects/SpringBootProject/Primesolutions/PrimeSolutions/FileUpload

希望有帮助。