最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Transform Object in another object - Stack Overflow

programmeradmin1浏览0评论

Anyone can help me with this object? I'm gonna receive a object like this:

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

How can i remove those "S", "BOOL" before each value? I need a object like this:

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

I need to make a function that convert that object like i explain above. Any ideas? Thanks for the help : )

Anyone can help me with this object? I'm gonna receive a object like this:

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

How can i remove those "S", "BOOL" before each value? I need a object like this:

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

I need to make a function that convert that object like i explain above. Any ideas? Thanks for the help : )

Share Improve this question asked Nov 19, 2020 at 16:53 Rafael de CarvalhoRafael de Carvalho 5911 gold badge6 silver badges11 bronze badges 3
  • do you have only a single property in the most nested objects? – Nina Scholz Commented Nov 19, 2020 at 16:54
  • It looks like queuesLinks is supposed to be transformed into an object, but it would need some property names for the values. What's wrong with using regular JSON this looks like someone tried to reinvent it. – James Commented Nov 19, 2020 at 17:05
  • its a response of dynamodb stream. – Rafael de Carvalho Commented Nov 19, 2020 at 20:31
Add a ment  | 

4 Answers 4

Reset to default 6

Just take the values from the nested objects.

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; }

You can do this

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

If you need a new object with the indicated structure you can try this:

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

This is another approach using reduce method!

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)
发布评论

评论列表(0)

  1. 暂无评论