如何获取对象数组中对象的所有属性值的数组?

时间:2018-06-04 19:15:07

标签: javascript arrays ecmascript-6 javascript-objects

我想获得每个对象的值数组。

我有这个:

const numDataPoints = [
    {
      x0: {x: 807, y: 625},
      x1: {x: 15, y: 20},
      x2: {x: 5, y: 20}
    },
    {
      x0: {x: 11, y: 6},
      x1: {x: 16, y: 21},
      x2: {x: 7, y: 22}
    }
  ];

我想要这个:

[
  [807, 625],
  [15, 20],
  [5, 20],
  [11, 6],
  [16, 21],
  [7, 22]
]

我试过了:

numDataPoints.map((array) => array.map(axis => [axis.x, axis.y]));

但它会抛出此错误:

  

未被捕捉TypeErrorarray.map不是函数

8 个答案:

答案 0 :(得分:7)

您可以将map方法与Object.values一起使用并传播语法...



const data =  [{ x0: {x: 807, y: 625}, x1: {x: 15, y: 20}, x2: {x: 5, y: 20} }, { x0: {x: 11, y: 6}, x1: {x: 16, y: 21}, x2: {x:7, y: 22} }];

const result = [].concat(...data.map(Object.values)).map(Object.values)
console.log(result)




答案 1 :(得分:5)

您可以使用Array.map将每个对象转换为双元素数组的数组,然后缩小以展平结果:



const numDataPoints =  [{ x0: {x: 807, y: 625}, x1: {x: 15, y: 20},
            x2: {x: 5, y: 20} }, { x0: {x: 11, y: 6}, x1: {x: 16, y: 21}, x2: {x:7, y: 22} }];

let result = numDataPoints.map(
    item => Object.values(item).map(({x, y})=> [x, y]))
              .reduce((arr, current) => [...arr, ...current], []);

  console.log(result);




答案 2 :(得分:4)

使用reduce也是如此 - 以下内容也适用于其他数据,因为我们不会在此处对任何密钥的名称进行硬编码。



const res = numDataPoints.reduce((a, b) => a.concat(Object.keys(b).map(e => Object.values(b[e]))), []);
console.log(res);

<script>
const numDataPoints = [
    {
        x0: {
            x: 807,
            y: 625
        },
        x1: {
            x: 15,
            y: 20
        },
        x2: {
            x: 5,
            y: 20
        }
    }, {
        x0: {
            x: 11,
            y: 6
        },
        x1: {
            x: 16,
            y: 21
        },
        x2: {
            x: 7,
            y: 22
        }
    }
];
</script>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

您的numDataPoints数组中没有数组,而是常规对象,因此您无法使用map

您需要的是Object.values。或者,要保证密钥x0x1x2 destructure{x0, x1, x2}的顺序相同,然后使用{ {1}}。

[x0, x1, x2]的结构表明,您实际上需要一组两个数组,每个数组都有三个 numDataPoints点,而不仅仅是六点[x, y]点。如果您仍想展平这些子阵列,请使用concatflatMap(目前是第3阶段候选人,可能成为2019年6月完成的ECMAScript版本的一部分)。

以下是所有六种可能性:

[x, y]

答案 4 :(得分:0)

您可以使用Object.keys迭代对象中的键,如下所示:

&#13;
&#13;
let data = [{
  x0: {
    x: 807,
    y: 625
  },
  x1: {
    x: 15,
    y: 20
  },
  x2: {
    x: 5,
    y: 20
  }
}, {
  x0: {
    x: 11,
    y: 6
  },
  x1: {
    x: 16,
    y: 21
  },
  x2: {
    x: 7,
    y: 22
  }
}];

let result = data.map(obj => Object.keys(obj).map((key) => [obj[key].x, obj[key].y]));

console.log(result);
&#13;
&#13;
&#13;

你可能想要缩小结果,我不确定。

答案 5 :(得分:0)

这是因为你得到的是一个对象,而不是一个数组。虽然你可以尝试(Es6):

numDataPoints
.map(_ => {
  return Object.values(_)
  .map(({x, y}) => [x, y]);
}).reduce((acc, elem) => [...acc, ...elem], []);

答案 6 :(得分:0)

问题是array是一个对象,因此您必须在使用另一个forEach之前映射密钥。

const numDataPoints =  [{ x0: {x: 807, y: 625}, x1: {x: 15, y: 20},x2: {x: 5, y: 20} }, { x0: {x: 11, y: 6}, x1: {x: 16, y: 21}, x2: {x:7, y: 22} }];
var foo = []
numDataPoints.forEach((points) => 
    Object.keys(points).forEach(point => 
        foo.push( [points[point].x, points[point].y] )
    )
);
console.log(foo)

答案 7 :(得分:0)

以下是获得两种不同结果的几种不同方法(原始嵌套结构或平面结构) - 另请注意可能是用例的排序 - 字典键按照声明的方式排序(不是字母数字) ):

DataGrid

相关问题