简体   繁体   English

将对象数组内的每个对象的键获取到数组中:Javascript

[英]Getting key of each object inside array of objects into an array: Javascript

I have an object with the following format 我有以下格式的对象

var obj = [{
  "a": 1
}, {
  "b": 2
}, {
  "c": 3
}];

Would want to fetch only keys out of each object inside this array of objects into a new array 想要只从该对象数组内的每个对象中提取键到新数组中

Something like this: ["a","b","c"] 像这样的东西: ["a","b","c"]

Have tried the following but it is not working : 尝试了以下方法,但是它不起作用:

 var obj = [{ "a": 1 }, { "b": 2 }, { "c": 3 }]; let result = obj.map (({ val }) => val) console.log(result); 

Merge to a single object by spreading into Object.assign() , and then get the keys: 通过扩展到Object.assign()合并到单个对象,然后获取键:

 var obj = [{"a":1},{"b":2},{"c":3}]; const result = Object.keys(Object.assign({}, ...obj)); console.log(result); 

Or use Array.flatMap() with Object.keys() : 或者将Array.flatMap()Object.keys()

 var obj = [{"a":1},{"b":2},{"c":3}]; const result = obj.flatMap(Object.keys); console.log(result); 

I'd .map and extract the first item in the Object.keys of the object being iterated over: 我将.map并提取要迭代的对象的Object.keys中的第一项:

 var obj = [{ "a": 1 }, { "b": 2 }, { "c": 3 }]; const result = obj.map(inner => Object.keys(inner)[0]); console.log(result); 

You can make use of Object.assign and spread(...) operator to solve this issue 您可以使用Object.assignspread(...)运算符来解决此问题

 var mainObj = [{"a":1},{"b":2},{"c":3},{"23":1},{"32b":2},{"232c":3}]; const allKeys = Object.keys(Object.assign({}, ...mainObj)); console.log(allKeys); 

You can simply use .map() to open the array and then use Object.keys to return the key from the object. 您可以简单地使用.map()打开数组,然后使用Object.keys从对象返回键。 Please refer the example I have made.I hope it helps 请参考我所做的示例。希望对您有所帮助

https://www.w3schools.com/code/tryit.asp?filename=G66AEBM9ZJCD https://www.w3schools.com/code/tryit.asp?filename=G66AEBM9ZJCD

let object1 = [{a:1,b:2,c:3}]
        let final = object1.map(function(object){ 
            return Object.keys(object)
        });

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

相关问题 如何在对象数组中分离每个对象的键和值-Javascript - How to separate key and value of each object inside array of objects - Javascript 如何在对象数组中为每个 object 添加带有值的键? - How to add the key with value for each object inside an array of objects? JavaScript:是否可以根据键访问对象数组中的对象? - JavaScript: Is it possible to access an object inside an array of objects based on its key? 删除对象数组中的对象键和值 - Remove object key and value inside of Array of Objects 获取对象内部数组中的对象总数 - Getting total objects in array inside object 获取对象内对象数组的长度 - getting the length of the array of objects inside an object 获取对象数组中 javascript object 的键名 - Get key name of a javascript object in an array of objects JavaScript 对象到具有“名称”键的对象数组 - JavaScript Object to Array of Objects with 'name' key 我有一个对象数组,每个对象内都有一个称为分数的键,我需要找到每个分数的总和并将其推入数组 - I have an array of objects, inside each object is key called scores, and I need to find the sum of each score and push it into an array JavaScript-在对象数组中的对象数组中查找对象 - JavaScript - Find an object among an array of objects, inside an array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM