简体   繁体   English

从对象中省略展开数组

[英]Omitting spread array from object

I have a function:我有一个功能:

const mergedPartOfStateWithCurrentSearchValues = (object, valuesToOmit) => {
  const {...valuesToOmit, ...rest } = object;
  return rest;
};

The first argument is an object.第一个参数是一个对象。 The second argument is an array of string values to omit from the object.第二个参数是要从对象中省略的字符串值数组。 I'm trying to return the rest.我正在尝试返回其余部分。 So this actually doesn't work like that.所以这实际上并不像那样工作。 I've tried to use lodash as well:我也尝试使用 lodash:

const mergedPartOfStateWithCurrentSearchValues = (object, valuesToOmit) => {
  const filteredObject = pickBy(object, function (value, key) {
    return !valuesToOmit.includes(key)
  })
  return filteredObject;
};

And it doesn't work as well.它也不起作用。 I'm running out of ideas.我的想法不多了。 How can I do what I want to and keeping the generic nature of this function?我怎样才能做我想做的事并保持这个功能的通用性?

You could take the entries of your object using Object.entries() and then filter out the entries which keys appear in your valuesToOmit array by using the .includes() and .filter() methods.您可以使用Object.entries()对象的条目,然后使用.includes().filter()方法过滤掉出现在valuesToOmit数组中的键的条目。 Then, you can use Object.fromEntries() to build an object from these filtered entries:然后,您可以使用Object.fromEntries()从这些过滤的条目中构建一个对象:

 const object = {a: 1, b: 2, c: 3}; const valuesToOmit = ['a', 'c'] const mergedPartOfStateWithCurrentSearchValues = (object, valuesToOmit) => { return Object.fromEntries( Object.entries(object).filter(([k]) => !valuesToOmit.includes(k)) ); } const res = mergedPartOfStateWithCurrentSearchValues(object, valuesToOmit); console.log(res);

If you can't support Object.fromEntries() , you could map the entries returned by Object.entries() to objects, and then merge them into a larger object using Object.assign() :如果您不能支持Object.fromEntries() ,您可以将Object.entries()返回的条目映射到对象,然后使用Object.assign()将它们合并为一个更大的对象:

 const object = {a: 1, b: 2, c: 3}; const valuesToOmit = ['a', 'c'] const mergedPartOfStateWithCurrentSearchValues = (object, valuesToOmit) => { return Object.assign( ...Object.entries(object).filter(([k]) => !valuesToOmit.includes(k)).map(([k, v]) => ({[k]: v})) ); } const res = mergedPartOfStateWithCurrentSearchValues(object, valuesToOmit); console.log(res);

Note : You can improve the efficiency of the above method by making valuesToOmit an ES6 Set , and then using the .has() method rather than the .includes() method.注意:您可以通过将valuesToOmit 设为ES6 Set ,然后使用.has()方法而不是.includes()方法来提高上述方法的效率。

You could reduce the array and get only an object without wanted keys.您可以减少数组并只获得一个没有想要的键的对象。

It works by removing a single key from the object in each loop by using a computed property and assigning to new variable name underscore, which is not used anymore and rest in object destructuring .它的工作原理是通过使用计算属性从每个循环中的对象中删除单个键并分配给新的变量名称下划线,下划线不再使用并停留在对象解构中

As result you get an object with without unwanted keys.结果你得到一个没有不需要的键的对象。

 const subSet = (object, without) => without.reduce((o, k) => ({ [k]: _, ...o } = o, o), object); console.log(subSet({ a: 1, b: 2, c: 3 }, ['a', 'c']));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM