【发布时间】:2021-09-16 00:28:52
【问题描述】:
我有一个用例,我必须重组对象。最初我有这个对象
{
name: 'Tommy Kerar',
mandatoryRequirements : [{
name: "dummy.pdf",
relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
},
{
name: "Sell the Solution, Not the Product.png",
relativeUrl:
"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
},
{
name: "ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
relativeUrl:
"2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=HjMt%2Fpo1xc%2Bzt%2BeWaN%2FpU3vpY4A%3D",
}],
postGrad: [
{
name: "dummy.pdf",
relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
},
{
name: "Sell the Solution, Not the Product.png",
relativeUrl:
"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
}
],
yourDocuments: [
{
name: "dummy.pdf",
relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
},
{
name: "Sell the Solution, Not the Product.png",
relativeUrl:
"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
}
]
}
结果输出我想要这样的东西 主要变化是我想在对象上方创建一个数组,名称为用户名,文件夹名称根据上述对象键,名称在下面的预期输出中。
PS:我是 JS 世界的新手。以前使用 Python。
const dd = {
name: 'Tommy Kerar',
mandatoryRequirements : [{
name: "dummy.pdf",
relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
},
{
name: "Sell the Solution, Not the Product.png",
relativeUrl:
"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
},
{
name: "ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
relativeUrl:
"2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=HjMt%2Fpo1xc%2Bzt%2BeWaN%2FpU3vpY4A%3D",
}],
postGrad: [
{
name: "dummy.pdf",
relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
},
{
name: "Sell the Solution, Not the Product.png",
relativeUrl:
"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
}
],
yourDocuments: [
{
name: "dummy.pdf",
relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
},
{
name: "Sell the Solution, Not the Product.png",
relativeUrl:
"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
url:
"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
}
]
}
这是我尝试过的,但坚持将username 和folder 添加到数组列表中。
let newA = [];
newA = dd.postGrad
newA = dd.yourDocuments
newA = dd.mandatoryRequirements
console.log(newA)
【问题讨论】:
-
欢迎来到 SO。当涉及到asking a good question 和这个question checklist 时,您可能会发现阅读网站help section 很有用。您为解决问题而编写的代码应包含minimal reproducible example。
-
你可以使用
Array.reduce(),或者循环对象并构造你的新对象 -
@boxdox 我尝试过,但无法按照我需要的方式操作它。 :(
-
请分享 @Andy 提到的 MRP (stackoverflow.com/help/minimal-reproducible-example)
-
您应该将您尝试过的代码添加到您的问题中。这可能是一个我们可以帮助解决的简单问题,但我们一般不会为他们编写其他人的代码。
标签: javascript arrays json reactjs ecmascript-6