变换另一个对象中的对象



有人能帮我处理这个对象吗?我会收到这样一个物体:

NewImage: {
sourceId: {
S: "some_string"
},
ignored: {
BOOL: false
},
stepFunctionArn: {
S: "some_string"
},
certificate: {
BOOL: true
},
infoNeeded: {
L: [
"Array"
]
},
queuesLinks: {
M: [
"Object"
]
},
}

我怎样才能去掉那些";S"BOOL";在每个值之前?我需要一个这样的对象:

NewImage: {
sourceId: "some_string",
ignored: false,
stepFunctionArn: "some_string"
...
}

我需要制作一个函数来转换这个对象,就像我上面解释的那样。有什么想法吗?感谢您的帮助:(

只需从嵌套对象中获取值。

const
data = { sourceId: { S: "some_string" }, ignored: { BOOL: false }, stepFunctionArn: { S: "some_string" }, certificate: { BOOL: true }, infoNeeded: { L: ["Array"] }, queuesLinks: { M: ["Object"] } },
result = Object.fromEntries(Object
.entries(data)
.map(([k, o]) => [k, Object.values(o)[0]])
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您可以执行此

Object.keys(NewImage).forEach(key=>{
NewImage[key]=NewImage[key][Object.keys(NewImage[key])[0]];
});
console.log(NewImage);

如果您需要一个具有指定结构的新对象,您可以尝试以下操作:

const newObj = Object.keys(NewImage).reduce((curr, next) => {
const key = Object.keys(NewImage[next])[0];
curr[next] = NewImage[next][key];
return curr; 
}, {})

这是另一种使用reduce方法的方法!

const data = {
sourceId: {
S: "some_string"
},
ignored: {
BOOL: false
},
stepFunctionArn: {
S: "some_string"
},
certificate: {
BOOL: true
},
infoNeeded: {
L: [
"Array"
]
},
queuesLinks: {
M: [
"Object"
]
},
};
const newObject = Object.entries(data).reduce(function(accumulator, currentValue) {
accumulator[currentValue[0]] = Object.values(currentValue[1])[0];
return accumulator;
}, {});
console.log(newObject)

最新更新