0

i have JSON array object coming as request with many fields. I have to send backend request with only limited fields how to achieve this using javascript.

This is how the request look like

{
   
   "Items":[
      {
         "ItemId":"1",
         "manufacturingNo": "Y23",
         "catalogID": "123",
         "quantity":2
         
      },
      {
         "ItemId":"2",
         "manufacturingNo": "Y33",
         "catalogID": "23",
         "quantity":29
         
      }
   ]
}

Required request is

{
   
   "viewItems":[
      {
         "ItemId":"1",
         "quantity":2
         
      },
      {
         "ItemId":"2",
         "quantity":29
         
      }
   ]
}

how to loop through and achieve this

1

1 Answer 1

2

const array = {
   
   "Items":[
      {
         "ItemId":"1",
         "manufacturingNo": "Y23",
         "catalogID": "123",
         "quantity":2
         
      },
      {
         "ItemId":"2",
         "manufacturingNo": "Y33",
         "catalogID": "23",
         "quantity":29
         
      }
   ]
}
const result = {
    viewItems: array.Items.map((item) => ({ ItemId: item.ItemId, quantity: item.quantity  }))
}
console.log(result)

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.