英文:
How to extract specific from an object
问题
我应该如何从数组中移除一个属性?我希望移除包括以下属性的对象:
someObject = { number: '1', month: 'march', year: '2023' }
而我有一个数组:
someArray = ['month', 'year'] 或一个字符串 'month, year'
并希望提取以获得:
finalObject = { month: 'march', year: '2023' }
英文:
How can I remove an property from an array? I wish to remove the property that includes:
someObject = { number: '1', month: 'march', year: '2023' }
And I have an array:
someArray = ['month', 'year'] or a string 'month, year'
And want to extract so that I get:
finalObject = { month: 'march', year: '2023' }
答案1
得分: 2
使用Object.entries
循环遍历键和值
,借助filter
只保留存在于propertiesToExtract
中的键,并将它们转换回对象,使用Object.fromEntries
。
const someObject = { number: '1', month: 'march', year: '2023' }
const propertiesToExtract = ['month', 'year']; //如果它是字符串 'month, year',可以使用 .split(',') 转换为数组;
const finalObject = Object.fromEntries(Object.entries(someObject).filter(([key, value]) => propertiesToExtract.includes(key)));
console.log(finalObject); // { month: 'march', year: '2023' }
英文:
loop over keys and values
with Object.entries
with the help of filter
to let only keys that exist in propertiesToExtract
and turn them back into object with Object.fromEntries
const someObject = { number: '1', month: 'march', year: '2023' }
const propertiesToExtract = ['month', 'year']; //if it is string 'month, year' turn it into array with the help of .split(', ');
const finalObject = Object.fromEntries(Object.entries(someObject).filter(([key,value])=>propertiesToExtract.includes(key)));
console.log(finalObject); // { month: 'march', year: '2023' }
答案2
得分: 1
要根据特定属性从数组中移除对象,您可以使用filter()
方法和includes()
方法。以下是如何实现所需结果的方法:
const someObject = { number: '1', month: 'march', year: '2023' };
const someArray = ['month', 'year']; // 或者 const someArray = 'month, year';
// 如果需要,将someArray字符串转换为数组
const propertiesToRemove = Array.isArray(someArray) ? someArray : someArray.split(', ');
// 使用Object.keys()获取someObject的键数组。我们使用includes()方法基于propertiesToRemove来过滤这个数组。
const finalObject = Object.keys(someObject)
.filter(key => propertiesToRemove.includes(key))
.reduce((obj, key) => {
obj[key] = someObject[key];
return obj;
}, {});
console.log(finalObject);
在这段代码中,首先我们根据提供的内容定义了someObject
和someArray
变量。然后,如果someArray
是字符串格式,我们将其转换为数组。
接下来,我们使用Object.keys()
获取了someObject
的键数组。然后,我们使用filter()
方法基于propertiesToRemove
来过滤这个数组。最后,我们使用reduce()
方法通过迭代过滤后的键来创建一个新对象finalObject
,并将someObject
中相应的值赋给finalObject
。
最后,我们将finalObject
记录在控制台中,其中只包含在someArray
或someArray
字符串中指定的属性。
注意:此代码假定要删除的属性存在于someObject
中。如果某些属性可能不存在于someObject
中,您可以添加额外的检查来处理这种情况。
英文:
To remove an object from an array based on specific properties, you can use the filter() method along with the includes() method. Here's how you can achieve the desired result:
const someObject = { number: '1', month: 'march', year: '2023' };
const someArray = ['month', 'year']; // or const someArray = 'month, year';
// Convert the someArray string to an array if needed
const propertiesToRemove = Array.isArray(someArray) ? someArray : someArray.split(', ');
// Filter the properties of someObject based on propertiesToRemove
const finalObject = Object.keys(someObject)
.filter(key => propertiesToRemove.includes(key))
.reduce((obj, key) => {
obj[key] = someObject[key];
return obj;
}, {});
console.log(finalObject);
In this code, we first define the someObject and someArray variables as provided. Then, we convert the someArray string to an array if it is in string format.
Next, we use Object.keys() to get an array of keys from someObject. We filter this array based on whether the properties in propertiesToRemove are present using the includes() method. Then, using reduce(), we create a new object finalObject by iterating over the filtered keys and assigning the corresponding values from someObject to the finalObject.
Finally, we log the finalObject to the console, which will contain only the properties specified in someArray or someArray string.
Note: The code assumes that the properties to remove exist in someObject. If there's a possibility that some properties might not exist in someObject, you can add additional checks to handle such cases.
答案3
得分: 1
我们可以编写一个函数,将您的键映射到[key, value]
子数组中,使用初始对象提取值,然后使用Object.fromEntries
将它们组合成一个新对象:
const extract = (object, keys) =>
Object.fromEntries(keys.map(key => [key, object[key]]))
const someObject = { number: '1', month: 'march', year: '2023' }
const someArray = ['month', 'year']
console.log(extract(someObject, someArray))
更新
有评论要求仅包括那些实际出现在原始对象中的键。这只是一个小调整:
const extract = (object, keys) =>
Object.fromEntries(keys.flatMap(key => key in object ? [[key, object[key]]] : []))
英文:
We can write a function that maps your keys into [key, value]
subarrays, using the initial object to extract the values, then use Object.fromEntries
to combine these into a new object:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const extract = (object, keys) =>
Object.fromEntries(keys.map(key => [key, object[key]]))
const someObject = { number: '1', month: 'march', year: '2023' }
const someArray = ['month', 'year']
console.log(extract(someObject, someArray))
<!-- end snippet -->
Update
A comment asked to include only those keys whose values actually appear in the original object. This is only a minor tweak:
const extract = (object, keys) =>
Object.fromEntries(keys.flatMap(key => key in object ? [[key, object[key]]] : []
))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论