javascript 如何在ES6中使用对象的值过滤对象

6psbrbz9  于 2023-01-24  发布在  Java
关注(0)|答案(8)|浏览(260)

在ES6中以这种方式过滤对象的最佳方法是什么?
起始数据:

const acceptedValues = ["value1","value3"]
const myObject = {
    prop1:"value1",
    prop2:"value2",
    prop3:"value3"
}

预期产出:

filteredObject = {
    prop1:"value1",
    prop3:"value3"
}
toiithl6

toiithl61#

可以使用reduce()创建新对象,使用includes()检查数组中是否存在对象的值。

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)
uklbhaso

uklbhaso2#

对于ES6,如果

  • 您的任务可以通过仅基于键和的过滤来解决
  • 您需要静态代码(您确切地知道需要筛选哪些属性),并且
  • 它不依赖于APP状态,

那么你可以使用以下的重构技术:

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

console.info({ filteredObject, prop2 })

您将拥有:

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

wydwbb8l3#

为了在@Nenad Vracar的基础上构建一个好答案,您可以使用一个对象来代替带有includes的Array,以加快查找速度:

const acceptedValues = ["value1", "value3"];
const myObject = {
  prop1: "value1",
  prop2: "value2",
  prop3: "value3"
};

const lookup = acceptedValues.reduce( (memo, prop) => {
  memo[prop] = true;
  return memo;
});

const filteredObject = Object.keys(myObject).reduce((filtered, key) => {
  if(lookup[myObject[key]]){
    filtered[key] = myObject[key];
  }
  return filtered;
}, {});

console.log(filteredObject);

也不是说includes不能完成这项工作,但我想提供一个替代视图。

x7yiwoj4

x7yiwoj44#

为什么不使用简单的for循环呢?

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);
b09cbbtk

b09cbbtk5#

由于我还没有看到使用Object.entries的答案,这里有一个。注意,由于Object.entries()的实现明显比Object.keys()慢,这也将比公认的答案慢,但有些人可能更喜欢这种可读性或可扩展性(更容易通过不同的过滤函数)。

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);

或者用一句比较长的俏皮话来说:

const filteredObject = Object.fromEntries(Object.entries(myObject).filter(([, v]) => accepted.includes(v)));
lpwwtiir

lpwwtiir6#

使用一个简单的for循环并按键获取对象。

const acceptedValues = ["value1","value3"]
const myObject = {
    prop1:"value1",
    prop2:"value2",
    prop3:"value3"
}

Object.prototype.getKeyByValue = function( value ) {
    for( var prop in this ) {
        if( this.hasOwnProperty( prop ) ) {
             if( this[ prop ] === value )
                 return prop;
        }
    }
}

for (var i in acceptedValues) {
  if (myObject.getKeyByValue(acceptedValues[i])){
    console.log(acceptedValues[i]);
  }
}
kx7yvsdv

kx7yvsdv7#

IMO,“最好的方法”是Lodash方法

const filtered = _.pick(myObject, acceptedValues)

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

hof1towb

hof1towb8#

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);

相关问题