Home > Net >  add properties to output object and convert to array of object
add properties to output object and convert to array of object

Time:08-22

This is my request body which I am sending to back end,

{
    "appId":'10001',
    "upstream":[
        {
          "name":"john",
          "content":"APPLICATION 1"
        },
        {
          "name":"peter",
          "content":"APPLICATION 2"
        },
    ],
    "downstream":[
        {
          "name":"max",
          "content":"APPLICATION 3"
        },
{
          "name":"justin",
          "content":"APPLICATION 4"
        },

    ],
}

I want to convert the same like below,

[
 {
    "appId":'10001',
    "name":"john",
    "content":"APPLICATION 1",
    "type":"upstream"
 },
 {
    "appId":'10001',
    "name":"peter",
    "content":"APPLICATION 2",
    "type":"upstream"
 },
 {
    "appId":'10001',
    "name":"max",
    "content":"APPLICATION 3",
    "type":"downstream"
 },
 {
    "appId":'10001',
    "name":"justin",
    "content":"APPLICATION 4",
    "type":"downstream"
 },
]

This needs to be done using Javascript (ES6).with the help of map function or any other ES6 utilities.

Thanks in advance.

CodePudding user response:

An improved solution using modern syntax can be:

const input = {
  appId: "10001",
  upstream: [
    {
      name: "john",
      content: "APPLICATION 1",
    },
    {
      name: "peter",
      content: "APPLICATION 2",
    },
  ],
  downstream: [
    {
      name: "max",
      content: "APPLICATION 3",
    },
    {
      name: "justin",
      content: "APPLICATION 4",
    },
  ],
};

const upstreams = input.upstream.map((v) => ({ appId: input.appId, ...v, type: "upstream" }));
const downstreams = input.downstream.map((v) => ({ appId: input.appId, ...v, type: "downstream" }));
const results = [...upstreams, ...downstreams];

console.log(results);

Here, we're creating a new array upstreams, which will be filled with the results of calling the first array of objects in input and adding to it additional key-value pairs (appId and the appropriate type). Same for the downstreams array.

We used the spread operator ... to quickly copy all of an existing array/object into another array/object.

And the keyword const instead of var since we're not going to mutate all used symbols.

CodePudding user response:

If you only want to convert it -

var input = {
    "appId": '10001',
    "upstream": [
        {
            "name": "john",
            "content": "APPLICATION 1"
        },
        {
            "name": "peter",
            "content": "APPLICATION 2"
        },
    ],
    "downstream": [
        {
            "name": "max",
            "content": "APPLICATION 3"
        },
        {
            "name": "justin",
            "content": "APPLICATION 4"
        },
    ],
};

var upstreams = input.upstream.map(v => { return {appId: input.appId, name: v.name, content: v.content, type: 'upstream'} });
var downstreams = input.downstream.map(v => { return {appId: input.appId, name: v.name, content: v.content, type: 'downstream'} });
var result = upstreams.concat(downstreams);
console.log(result);
  • Related