简体   繁体   English

从动态对象数组中,如何将 mutltple 属性的值提取为数组

[英]From an array of dynamic objects, how to extract value of a mutltple property as array

I have a dynamic JavaScript object array with the following structure:我有一个具有以下结构的动态 JavaScript 对象数组:

var someJsonArray = [
  {point: 100, level: 3},
  {point: 100, level: 3},
  {point: 300, level: 6}
];

and sometime it would be with some other combination like:有时它会与其他一些组合,如:

var someJsonArray = [
      {discussion: 5, level: 3},
      {discussion: 4, level: 3},
      {discussion: 3, level: 6}
    ];

I want to extract a field from each object, and get an array containing the values, for example from the fist array would give array as [[100, 3],[100, 3], [300, 6]] .我想从每个对象中提取一个字段,并获取一个包含这些值的数组,例如从 fist 数组中将数组作为[[100, 3],[100, 3], [300, 6]]

If there would be some static array I would have pushed that into one array using a loop like below:如果有一些静态数组,我会使用如下循环将其推送到一个数组中:

var arr = [];
for(var i=0; i<someJsonArray.length; i++) {
    arr.push(someJsonArray[i].id, someJsonArray[i].name);
}
console.log(arr)

But in my case the array is dynamic and there could be any combination of property, but there will be only two property in each combination.但在我的情况下,数组是动态的,可以有任何属性组合,但每个组合中只有两个属性。

How to get the array of value objects from the dynamic array?如何从动态数组中获取值对象数组? Any help or workaround for this?对此有任何帮助或解决方法吗?

You can use Object.values :您可以使用Object.values

 var arr = [ {point: 100, level: 3}, {point: 100, level: 3}, {point: 300, level: 6} ]; const res = arr.map(Object.values); console.log(res);

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

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