How to filter an object with its values in ES6

前端 未结 8 1561
日久生厌
日久生厌 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:15

    IMO, the "best way" is the Lodash way

    const filtered = _.pick(myObject, acceptedValues)
    

    https://lodash.com/docs/4.17.10#pick

    0 讨论(0)
  • 2020-12-11 02:15
    function filter(myObject){
      var obj=Object.assign({},myObject);
      Object.keys(obj).forEach(function(key) {
          if(acceptedValues.indexOf(obj[key])<0) delete obj[key];
      });
      return obj;
    }
    const filteredObject=filter(myObject);
    
    0 讨论(0)
  • 2020-12-11 02:21

    Since I haven't seen an answer using Object.entries here's one. Note, due to Object.entries() implementation being significantly slower than Object.keys(), this will also be slower than the accepted answer, but some may prefer this for readability or extendability (easier to pass a different filtering function).

    const acceptedValues = ["value1", "value3"];
    const myObject = {
        prop1:"value1",
        prop2:"value2",
        prop3:"value3"
    };
    const filteredEntries = Object.entries(myObject).filter(([, v]) => acceptedValues.includes(v));
    const filteredObject = Object.fromEntries(filteredEntries);
    

    Or as a longish one-liner:

    const filteredObject = Object.fromEntries(Object.entries(myObject).filter(([, v]) => accepted.includes(v)));
    
    0 讨论(0)
  • 2020-12-11 02:24

    Why not a simple for loop?

    const acceptedValues = ["value1","value3"]
    const myObject = {
        prop1:"value1",
        prop2:"value2",
        prop3:"value3"
    };
    var  filteredObject = {};
    for(e in myObject) {
        if (myObject.hasOwnProperty(e)) {
          if (acceptedValues.indexOf(myObject[e]) != -1) {
              filteredObject[e] = myObject[e];
          }
        }
    }
    console.log(filteredObject);

    0 讨论(0)
  • 2020-12-11 02:25

    You can use reduce() to create new object and includes() to check if value of object exists in array.

    const acceptedValues = ["value1", "value3"]
    const myObject = {
      prop1: "value1",
      prop2: "value2",
      prop3: "value3"
    }
    
    var filteredObject = Object.keys(myObject).reduce(function(r, e) {
      if (acceptedValues.includes(myObject[e])) r[e] = myObject[e]
      return r;
    }, {})
    
    console.log(filteredObject)

    0 讨论(0)
  • 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"
    
    0 讨论(0)
提交回复
热议问题