0

My API returns below data in nodejs.

console.log(body) prints the below.

{
  "items": [
    {
      "PlanId": 2007,
      "PlanCode": "Future Mat Cost Planning  - Budget",
      "Description": "Future Mat Cost Planning  - Budget",
      "FromMonth": "2019-10-01T00:00:00+00:00"
    },
    {
      "PlanId": 3001,
      "PlanCode": "Nvidia Cost PL",
      "Description": "Nvidia Cost PL",
      "FromMonth": "2019-10-01T00:00:00+00:00"

    },
    {
      "PlanId": 1001,
      "PlanCode": "Material Cost Planning - PO",
      "Description": "Material Cost Planning - PO",
      "FromMonth": "2019-10-01T00:00:00+00:00"
    }
   ],
 "count": 5,
  "hasMore": true,
  "limit": 5,
  "offset": 0
}

I have to create object in which I have to store PlanId and plancode. How can i do that. I need to store data in key value format. can anybody help me to create either array or object? I am new to nodejs I want result like this.

obj=[{
      "PlanId": 3001,
      "PlanCode": "Nvidia Cost PL",
     },
    {
      "PlanId": 1001,
      "PlanCode": "Material Cost Planning - PO",
    }
    ];
2
  • 1
    What are expected results? Commented Jun 7, 2020 at 22:53
  • @charlietfl I have modified the code Commented Jun 7, 2020 at 23:19

1 Answer 1

1

so you want to map the response to a new object?

// assuming `body` is the API response

const obj = body
  .items
  .map(item => ({
    PlanId: item.PlanId,
    PlanCode: item.PlanCode,
  });
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.