How to delete property from spread operator?

2020-07-13 07:48发布

I want to delete drugName from the response but it is not happening any idea how to delete property from spread operator ? main.js

  const transformedResponse = transformResponse(response);
  const loggerResponse = {...transformedResponse};
  delete loggerResponse[drugName];
  console.log("LOGGER>>>>", loggerResponse);
  logger().info('Drug Price Response=', { ...loggerResponse, memberId: memberId, pharmacyId: pharmacyId });

\ data

LOGGER>>>> {
    '0': {
        isBrand: false,
        drugName: 'test drug',
        drugStrength: '5 mg 1 5 mg',
        drugForm: 'Tablet',
    }
}

transformResponse

[{
    drugName: 'HYDROCODONE-HOMATROPINE MBR',
    drugStrength: '5MG-1.5MG',
    drugForm: 'TABLET',
    brand: false
}]

5条回答
对你真心纯属浪费
2楼-- · 2020-07-13 08:29

This is the most succinct and immutable way that I've found. You simply destructure the object into two parts: one part is the property you're trying to remove (drugName in this case), and the other part is the rest of the object, that you want to keep (drugWithoutName in this case).

Once you've done that, you can abandon the property that has been removed, abandon the original object, and use the new object (drugWithoutName in this case) that has all of the remaining fields.

Coming up with the syntax isn't obvious, but it makes sense once you see it:

const response = [{
    drugName: 'HYDROCODONE-HOMATROPINE MBR',
    drugStrength: '5MG-1.5MG',
    drugForm: 'TABLET',
    brand: false
}]

console.log("response", response)

const removeNamesFromResponse = (response) => {
  return response.map(drug => {
    const { drugName, ...drugWithoutName } = drug
    return drugWithoutName
  })
}

const responseWithoutNames = removeNamesFromResponse(response)

console.log("responseWithoutNames", responseWithoutNames)

These articles explain the concept further:

https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90

https://github.com/airbnb/javascript/blob/master/README.md#objects--rest-spread

查看更多
走好不送
3楼-- · 2020-07-13 08:40

You could use Rest syntax in Object Destructuring to get all the properties except drugName to a rest variable like this:

const transformedResponse = [{
    drugName: 'HYDROCODONE-HOMATROPINE MBR',
    drugStrength: '5MG-1.5MG',
    drugForm: 'TABLET',
    brand: false
},
{
    drugName: 'HYDROCODONE ABC',
    drugStrength: '10MG',
    drugForm: 'SYRUP',
    brand: true
}]

const output = transformedResponse.map(({ drugName, ...rest }) => rest)

console.log(output)

Also, when you spread an array inside {}, you get an object with indices of the array as key and the values of array as value. This is why you get an object with 0 as key in loggerResponse:

const array = [{ id: 1 }, { id: 2 }]
console.log({ ...array })

查看更多
甜甜的少女心
4楼-- · 2020-07-13 08:42

1 line solution using ES9 Object Rest Operator

const loggerResponse = {
  "0": {
    isBrand: false,
    drugName: "test drug",
    drugStrength: "5 mg 1 5 mg",
    drugForm: "Tablet",
  },
};
const { drugName, ...newResponse } = loggerResponse["0"];
console.log(newResponse);

查看更多
\"骚年 ilove
5楼-- · 2020-07-13 08:52

Another option is to write a generic function, removeKey -

const removeKey = (k, { [k]:_, ...o }) =>
  o

const values =
  [ { a: 1, x: 1 }
  , { a: 1, y: 1 }
  , { a: 1, z: 1 }
  ]

console .log (values .map (v => removeKey ("a", v)))
// [ { x: 1 }, { y: 1 }, { z: 1 } ]

The function can be easily adapted to remove multiple keys, if necessary -

const removeKey = (k = "", { [k]:_, ...o } = {}) =>
  o

const removeKeys = (keys = [], o = {}) =>
  keys .reduce ((r, k) => removeKey (k, r), o)

const values =
  [ { a: 1, x: 1 }
  , { a: 1, y: 1 }
  , { a: 1, z: 1 }
  ]

console .log (values .map (v => removeKeys (['a', 'z'], v)))
// [ { x: 1 }, { y: 1 }, {} ]

查看更多
老娘就宠你
6楼-- · 2020-07-13 08:54

If you have property drugName in every object of transformedResponse array you can use Array.map() to generate a new array without drugName property:

Example:

const transformedResponse = [
  {
    drugName: 'HYDROCODONE-HOMATROPINE MBR',
    drugStrength: '5MG-1.5MG',
    drugForm: 'TABLET',
    brand: false
  },
  {
    drugName: 'TEST',
    drugStrength: '7MG-1.9MG',
    drugForm: 'TABLET',
    brand: false
  }
];

const loggerResponse = transformedResponse.map(o =>
{
    let clone = Object.assign({}, o);
    return (delete clone.drugName, clone);
});

console.log(loggerResponse);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

查看更多
登录 后发表回答