1

First object is a map (with 1 element only for make it simple):

let dataMap = {
      Feed: "data",
    };

I need to assign those key values to separated path:

Object.keys(dataMap).forEach((value) => {
  struct[value] = Object.assign({}, struct[value], {
    "State": obj.Digital[dataMap[value]].State,
  });
});

console.log(struct);

Now the result looks something like this:

struct: { 
 Feed: { 
  State: "xy"
  } 
}

What I would like to do, is to change the Feed key to Feed.next. So it would look like:

struct: { 
     Feed: {
      next: {
       State: "xy"
       } 
      }
     }

Please note that struct.Feed does exist already from the beginning, so this is not created with the code above. My question is how should I write the dataMap object to achieve the structure above?

Is it like (it is not working):

let dataMap = {
          Feed[next]: "data",
        };

Thank you,

3
  • where do i find the path? Commented Nov 6, 2017 at 10:28
  • hmm, I mean, in the first solution inside the forEach method, struct[value] is equal to struct.Feed. What I would like to get is struct[value] equal to struct.Feed.next Commented Nov 6, 2017 at 10:37
  • please add what you have and what you want. Commented Nov 6, 2017 at 10:42

1 Answer 1

1

Something like this:

var struct =  {Feed:{ State: "xy"}};

Add "next" property:

struct.Feed.next = {};

Create a copy of State in struct.Feed.next:

struct.Feed.next = JSON.parse(JSON.stringify(struct.Feed));

Delete fields:

delete struct.Feed.State
delete struct.Feed.next.next

Object now is:

struct {Feed: next{State: "xy"}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.