FabricioG
FabricioG

Reputation: 3320

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. How can achieve this?

Upvotes: 0

Views: 61

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370769

Map each object to just its one value first, and then you'll have a plain array of values you can manipulate:

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]);

Upvotes: 2

Related Questions