简体   繁体   English

JS 仅返回特定 object 的值

[英]JS return value only of a specific object

I currently have an array of preset objects like so:我目前有一组预设对象,如下所示:

var productsImages = [
  {sg: ''}, {scamo: ''}, {schar: ''}, {mar: ''}, {tb: ''}, {tg: ''}, {tcamo: ''}, {tcamo: ''}, {tol: ''}, {fb: ''}, {fg: ''},
];

Each one of these will have a different value.这些中的每一个都将具有不同的价值。 They're going to be in a specific order in the array.它们将在数组中按特定顺序排列。 I need to return the value of each one for example.例如,我需要返回每个值。

productsImages[index]

What I'm looking for is something like productsImages[0] == give me just the value not the whole object.我正在寻找的是 productsImages[0] == 给我的值而不是整个 object。 How can achieve this?怎样才能做到这一点?

Map each object to just its one value first, and then you'll have a plain array of values you can manipulate: Map 每个 object 首先只有一个值,然后您将拥有一个可以操作的普通值数组:

 var productsImages = [ {sg: 'foo'}, {scamo: ''}, {schar: ''}, {mar: ''}, {tb: ''}, {tg: ''}, {tcamo: ''}, {tcamo: ''}, {tol: ''}, {fb: ''}, {fg: ''}, ]; const values = productsImages.map(obj => Object.values(obj)[0]); console.log(values[0]);

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

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