ES6中过滤对象的最佳方法是什么?
起始数据:
const acceptedValues = ["value1", "value3"]
const myObject = {
prop1: "value1",
prop2: "value2",
prop3: "value3"
}
期望输出:
filteredObject = {
prop1: "value1",
prop3: "value3"
}
ES6中过滤对象的最佳方法是什么?
起始数据:
const acceptedValues = ["value1", "value3"]
const myObject = {
prop1: "value1",
prop2: "value2",
prop3: "value3"
}
期望输出:
filteredObject = {
prop1: "value1",
prop3: "value3"
}
您可以使用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)
.filter
不起作用,当我有一个对象时? this.activeUsers = window.users.filter( function(user) {// return ( (user.test === '0') && (user.isok === '0') ); return user.user_id === 1; });
出现错误.filter is not a function
。 - Imnotapotato对于ES6和if
那么您可以使用以下解构技术:
const myObject = {
prop1: 'value1',
prop2: 'value2',
prop3: 'value3'
}
const { prop2, ...filteredObject } = myObject
console.info({ filteredObject, prop2 })
然后你将会拥有:
filteredObject: {prop1: "value1", prop3: "value3"}
prop2: "value2"
为了进一步完善@Nenad Vracar的好答案,你可以使用对象而不是数组,并使用includes
进行更快的查找:
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
不能胜任这个工作,但我想提供一种替代方案。
鉴于我在这里没有看到使用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)));
fromEntries
建议,很不错。 - undefinedconst 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);
在我看来,“最好的方法”是使用 Lodash 的方式。
const filtered = _.pick(myObject, acceptedValues)
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]);
}
}
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);
const newObj = {};
for(let key of Object.keys(myObject)){
if(acceptedValues.includes(key)){
newObj[key] = myObject[key]
}
}
console.log(newObj);
Object
(而应该使用Array
)。 - user6445533