1

I have an object image whose key and value are as follows:

{ homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg' }

I want to extract the value of this object, namely "/assets/images/uploads/image-homeSliderImage.jpg", without looping through this object or using the object's key.

slevy1
  • 3,797
  • 2
  • 27
  • 33
David Ramirez
  • 207
  • 5
  • 18
  • and what have you tried it? can you show us some code? – JV Lobo Jul 12 '17 at 23:33
  • Can you give an example of the full object, the one you'd normally expect to loop through and could you also include your attempt, thank you. – NewToJS Jul 12 '17 at 23:33
  • yeah, that's the same you have written in your question @DavidRamirez we'd need some code – JV Lobo Jul 12 '17 at 23:39
  • So if it only contains one thing why would you need to loop through it? Or why would you want to try avoid looping through something that doesn't need to be looped? – NewToJS Jul 12 '17 at 23:42

4 Answers4

2

You can try 2 methods,

object[keys] 

If you dont know keys first fetch key.

my_dict = { homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg' }
key_li = Object.keys(my_dict)    
console.log(my_dict[key_li[0]])

Next method,

Use underscore.js

console.log(_.values(my_dict))

This will give you list of values. You can play in many with underscore.js

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

There are several ways you can the get value of the object property. Below is the simplest of all forms.

var obj = { homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg' };

console.log(obj.homeSliderImage);

DPatel
  • 71
  • 6
  • is there a way of getting the object without calling .homeSliderImage – David Ramirez Jul 12 '17 at 23:41
  • @DavidRamirez but why would you want to avoid using `homeSliderImage` it's the purpose of having it in the first place. Replace `console.log(obj.homeSliderImage);` with `console.log(obj[Object.keys(obj)[0]]);` – NewToJS Jul 12 '17 at 23:43
  • its because I'm looping through an object before this and im getting multiple `{ homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg' };` `{ Something1: '/assets/images/uploads/image-homeSliderImage.jpg' };` and every time around this loop the key changes so im trying to avoid calling the key to get the value of the object @NewToJS – David Ramirez Jul 12 '17 at 23:45
  • @DavidRamirez this is why it's important for you to include relevant source code to your question, it gives others something to debug and explain the reason for the unexpected output and it avoids people suggesting methods that might conflict with existing source code just like using `obj.homeSliderImage` I can only assume that is causing some conflict as it would be the only reason you wouldn't want to use it. – NewToJS Jul 12 '17 at 23:48
  • 1
    @DavidRamirez Well if the key value keeps changing and you don't know of the name of that key then you can use `obj[Object.keys(obj)[0]]` as I have suggested in one of my previous comments. **Exmple:** https://jsfiddle.net/n0kt3rub/ – NewToJS Jul 12 '17 at 23:50
  • hey @NewToJS It worked and sorry for not including relevant source code i should have explained the question more thoroughly. – David Ramirez Jul 12 '17 at 23:59
  • @DavidRamirez Why are the keys changing in the first place? An array of objects with different keys is usually bad design, it should just be a single object with multiple properties. – Barmar Jul 13 '17 at 00:42
0

Once you assign the object to a variable in JavaScript, you may directly access its value using array notation, as follows:

var o = {homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg'};

var image_path_name_value = o['homeSliderImage'];

console.log(image_path_name_value);

Note, you need the object's key to access its value. That key may be hard-coded as in this first example or you may attain it dynamically by fetching the object's solitary key, as follows:

var o = {homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg'};

var strKey = Object.keys(o)[0];

console.log( o[strKey] );

More about Object.keys()

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • 1
    `key` is an array, you need to index it: `o[key[0]]` – Barmar Jul 13 '17 at 00:24
  • It only works because when you convert an array to a string it does `array.join(',')`, and for a single-element array this happens to be that element. It feels wrong to write code that depends on this, when you can easily use an index to make it clear. – Barmar Jul 13 '17 at 00:38
  • Only when it's being used in a context that requires a string. `Object.keys(o).length` is not the same as `Object.keys(o)[0].length` – Barmar Jul 13 '17 at 01:04
  • Okay, I've revised my code based on your helpful comments -- thank you! – slevy1 Jul 13 '17 at 01:07
0

Given an object with a single property, the value can be accessed using Object.keys and the first (only) returned key:

var value = myObject[Object.keys(MyObject)[0]];

E.g.

var myObj = { homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg' };

console.log(myObj[Object.keys(myObj)[0]]);

Object properties are not ordered, so if more properties are added, any of them might be returned as the "first" key.

RobG
  • 142,382
  • 31
  • 172
  • 209