Gaurav
Gaurav

Reputation: 37

trim JS object to remove extra params with curly braces

I have an object response here

result.joblist = {
  "collection_job_status_list": [
    {
      "application_context": {
        "application_id": "a4",
        "context_id": "c4"
      },
      "creation_time": "15699018476102",
      "progress": 100,
      "status": "READY",
      "phase": "ACTIVE",
      "job_error": {}
    },
    {
      "application_context": {
        "application_id": "a6",
        "context_id": "c6"
      },
      "creation_time": "15698648632523",
      "progress": 100,
      "status": "READY",
      "phase": "ACTIVE",
      "job_error": {}
    }
  ],
  "result": {
    "request_result": "ACCEPTED",
    "error": {}
  }
}

Need to get rid of {"application_context": & ending } here, just need application_id":"a4","context_id":"c4" at the same level.

I have tried something like this, but not able to move ahead.

var newObj: any = {};
if (allJobs && allJobs.length > 0) {
  // this.rowData = this.allJobs;
  // this.allJobs = this.allJobs['application_id'];
  //let ele:object = allJobs.application_context;
  allJobs.forEach(ele => {
    newObj = {
      application_id: ele.application_context.application_id,
      context_id: ele.application_context.application_context
    };
    return newObj;
  });
}

Upvotes: 1

Views: 395

Answers (3)

Prasanna
Prasanna

Reputation: 4656

You need to use the map function

const result = {};
result.joblist = {
  "collection_job_status_list": [
    {
      "application_context": {
        "application_id": "a4",
        "context_id": "c4"
      },
      "creation_time": "15699018476102",
      "progress": 100,
      "status": "READY",
      "phase": "ACTIVE",
      "job_error": {}
    },
    {
      "application_context": {
        "application_id": "a6",
        "context_id": "c6"
      },
      "creation_time": "15698648632523",
      "progress": 100,
      "status": "READY",
      "phase": "ACTIVE",
      "job_error": {}
    }
  ],
  "result": {
    "request_result": "ACCEPTED",
    "error": {}
  }
}


result.joblist.collection_job_status_list = result.joblist.collection_job_status_list.map(item => {
  return {
     "application_id": item.application_context.application_id,
     "context_id": item.application_context.context_id
  }
})

console.log(result)

Or you can use map function along with shorthand es6 syntaxes

const result = {};
result.joblist = {
  "collection_job_status_list": [
    {
      "application_context": {
        "application_id": "a4",
        "context_id": "c4"
      },
      "creation_time": "15699018476102",
      "progress": 100,
      "status": "READY",
      "phase": "ACTIVE",
      "job_error": {}
    },
    {
      "application_context": {
        "application_id": "a6",
        "context_id": "c6"
      },
      "creation_time": "15698648632523",
      "progress": 100,
      "status": "READY",
      "phase": "ACTIVE",
      "job_error": {}
    }
  ],
  "result": {
    "request_result": "ACCEPTED",
    "error": {}
  }
}


result.joblist.collection_job_status_list = result.joblist.collection_job_status_list.map(({application_context}) => {
  return {
     ...application_context
  }
})

console.log(result)

EDIT:

The things you want to keep in your array depends on what you are returning from the map. You return the keys you want to keep. So if you want other items.

If you have an array A

A = [
  {
    nest: {
      x: 1,
      y: 1,
    },
    key1: 5,
    key2: 7,
  },
  {
    nest: {
      x: 1,
      y: 1,
    },
    key1: 5,
    key2: 7,
  },
  {
    nest: {
      x: 1,
      y: 1,
    },
    key1: 5,
    key2: 7,
  }
]

Let us assume you want x from nest, key1 and key2 in your final output. You will then do

const finalOutput = A.map(item => {
  return {
     x: item.nest.x, // x from nest
     key1: item.key1, // key1
     key2: item.key2,  // key2
  }
})

But there are shorthand forms for doing this. Let's assume that the item we get in our map function is already divided into a nest and a rest variable. The nest contains item.nest and the rest is {key1: 5,key2:7}, then you can simply return x from nest and everything else from rest

const finalOutput = A.map(({nest, ...rest}) => {
  return {
     x: nest.x, // x from nest
     ...rest, // everything else
  }
})

Upvotes: 0

Code Maniac
Code Maniac

Reputation: 37755

You can use map and destructuring

  • Get the collection_job_status_list from result
  • Loop over the values take out the required values from application_context key and merge with remaining values
  • Build the same structure as original result

let result = {"collection_job_status_list": [{"application_context": {"application_id": "a4","context_id": "c4"},"creation_time": "15699018476102","progress": 100,"status": "READY","phase": "ACTIVE","job_error": {}},{"application_context": {"application_id": "a6","context_id": "c6"},"creation_time": "15698648632523","progress": 100,"status": "READY","phase": "ACTIVE","job_error": {}}],"result": {"request_result": "ACCEPTED","error": {}}}

let { collection_job_status_list, ...rest } = result

let modified = collection_job_status_list.map(({
  application_context: {
    application_id,
    context_id
  },
  ...rest
}) => ({ ...rest, context_id, application_id}))

let final = {
  collection_job_status_list: modified,
  ...rest
}

console.log(final)

Upvotes: 2

dRoyson
dRoyson

Reputation: 1507

What you need here is a map. Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Try modifying the code as below and you should have your expected output.

var newObj: any = {};
var newArray;
if (allJobs && allJobs.length > 0) {
  newArray = allJobs.map(ele => {
    newObj = {
      application_id: ele.application_context.application_id,
      context_id: ele.application_context.application_context
    };
    return newObj;
  });
}

Hope this helps :)

Upvotes: 0

Related Questions