1

The array I have is as follows. Let's call this array reduction.

var reduction = [
    {
        ReductionID: 5813,
        PatiendID: 945994,
        ProviderAcctNumber: "",
        Discharge: "945994 : 01/01/0001 - 01/01/001",
        Selected: 1,
        Balance: 20,
        Charges: 10
    }
];

I want a new array (patientDetails) with only two values in the format

{
   PID: 945994 // which is PatientID in the reduction array
   selection: 1// which is Selected in the reduction array
}

What I want in the second array is :

  • Only two values from 1st array(reduction)
  • The two names should be different in 2nd array(ie., PatientID should ne PID)
  • Values are same as the values in 1st array

Is it possible to do with map() function ?

5
  • yes, it is possible, although you can do it with a very simple for loop. Have you tried? Commented Jan 23, 2020 at 11:39
  • I tried using map(). But assing values with adifferent name I didn't get Commented Jan 23, 2020 at 11:41
  • Please, show us your attemp. We expect it here in SO, since we don't like to write all code for users, we prefer to see some effort and then guide people in a way to fix issues Commented Jan 23, 2020 at 11:42
  • Dont use screenshots. Instead provide code so we can use it more easily to create our answeres. I've edited your question. Commented Jan 23, 2020 at 11:44
  • @CalvinNunes : it looks like you over-complicate ;) for some people it is just a low hanging fruit, traditionally (though unfair), OP gets downvoted, while couple of dudes score some rep for good-old Array.prototype.map() Commented Jan 23, 2020 at 12:22

2 Answers 2

7

You can do it like this

var arr = [{
    ReductionID:5813,
    PatientID: 945994,
    ProviderAcctNumber:"",
    Discharge: "945994 : 01/01/0001 - 01/01/001",
    Selected: 1,
    Balance: 20,
    Charges: 10
}];

var updatedArr = arr.map((obj)=>{
    return {
        PID:obj.PatientID,
        selection:obj.Selected
    }
});

console.log(updatedArr);

Here we iterate over the original array using map and select the values we need & return the Object as required

Sign up to request clarification or add additional context in comments.

Comments

5

map here is the best approach for you. You can simply do:

var reduction = [
    {
        ReductionID: 5813,
        PatiendID: 945994,
        ProviderAcctNumber: "",
        Discharge: "945994 : 01/01/0001 - 01/01/001",
        Selected: 1,
        Balance: 20,
        Charges: 10
    }
]
 const patientDetails = reduction.map(({PatiendID,Selected}) => ({PID: PatiendID, selection: Selected}))
 
 console.log(patientDetails) //  [{"PID": 945994, "selection": 1}]

Here callback function of map iterates over each elements reduction and returns a new object with only PID and selection

1 Comment

As long as you used destructuring, you could have gone all the way through: reduction.map(({PatientID:PID,Selected:selection}) => ({PID,selection}))

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.