How to filter an object with its values in ES6

前端 未结 8 1573
日久生厌
日久生厌 2020-12-11 01:53

What is the best way to filter an object this way in ES6?

Starting data:

const acceptedValues = [\"value1\",\"value3\"]
const myObject = {
    prop1:         


        
8条回答
  •  再見小時候
    2020-12-11 02:27

    For ES6 and if you need static code (you know exactly, what properties you need to filter) and it does not depends on the app state, than you can use the following destructuring technique:

    const myObject = {
      prop1: 'value1',
      prop2: 'value2',
      prop3: 'value3'
    }
    const { prop2, ...filteredObject } = myObject
    
    console.info({ filteredObject, prop2 })
    

    And you will have:

    filteredObject: {prop1: "value1", prop3: "value3"}
    prop2: "value2"
    

提交回复
热议问题