Extract image value from an object [duplicate] - javascript

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 5 years ago.
I have an object like this :
var obj = [
{
"id": 728,
"title": "A long day",
"images": {
"illustration": {
"title": "Preview",
"16x9": {
"1248x702": "https://example.com/image/225944d77559.jpg",
"1920x1080": "https://example.com/image/4546b05422594.jpg"
}
}
}
}
];
I'm trying for some time to get the 1920x1080 value, but without success :
alert(obj[0].title); //works
alert(obj[0].images.illustration.title); // works
alert(obj[0].images.illustration.16x9.1920x1080); // doesn't work and break the code
alert(obj[0].images.illustration.'16x9'.'1920x1080'); // doesn't work and break the code
I need your help. What should I do to get the 1920x1080 entry correctly ?

Use bracket:
var obj = [
{
"id": 728,
"title": "A long day",
"images": {
"illustration": {
"title": "Preview",
"16x9": {
"1248x702": "https://example.com/image/225944d77559.jpg",
"1920x1080": "https://example.com/image/4546b05422594.jpg"
}
}
}
}
];
console.log(obj[0].images.illustration['16x9']['1920x1080']);

Try this,
alert(obj[0].images.illustration['16x9']['1920x1080']);

You should write next:
obj[0].images.illustration['16x9']['1920x1080']
In JS if you have object key with specific sympols you should wrap this in Square brackets ['1920x1080'], its like you get elemet from array.

Related

Extracting specific value from json response [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I add a json value from a response to a variable and then print it this way result: ${JSON.stringify(result)}, but what i want exactly is to get only one exact value from this json
here's the json
{
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
I only want to extract lionsflag string only from the first flagname, this value is able to change any time
You need to use Object.keys() to get all the keys and then check the related value is meet your demand or not
let data = {
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
let value = "lionsflag"
data.flags.filter(d => Object.keys(d).forEach(e =>{
if(d[e] == value){
console.log(e + ";" + value)
}
}))
let prop ='flagname'
console.log(data.flags[0][prop])

Retrieve value from object having key and index as a string: 'array[0].key' [duplicate]

This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 4 years ago.
I'm working with some specific library for forms in javascript. In this library if I have:
{
"id": 1,
"order": 0,
"title": "A title",
},
I can do:
const field = 'order';
form.getState().values[field]
And I'll get the title value.
Now let's say that I have this data:
{
"id": 1,
"order": 0,
"title": "A title",
"Tags": [
{
"id": 1,
"name": "Tag one",
},
{
"id": 2,
"name": "Tag two",
}
]
},
And also I have access to a string with the name of the field for tags, the index of the array, and the field I want to retrieve. But I have all this in a string:
Tags[0].name
If I do:
const field = 'Tags[0].name';
form.getState().values[field]
It will return undefined, because will try to find the key Tags[0].name in the object. So: how can I do to retrieve the name property from a specific index in the Tags array, having Tags[0].name as a string?
A bit nasty but quick and working way to do it is to use eval:
const field = 'Tags[0].name';
let stateValues = form.getState().values;
let tagName = eval(`stateValues.${ field }`)
Other approach might be to split and parse the field path using regexp and then traverse the object property by property.

Javascript - How to write json path when there's an integer tag [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 6 years ago.
Javascript:
var json_obj =
{
"1111": {
"name": Bob,
"id": 1
},
"2222": {
"name": Alice,
"id": 2
}
}
var first_name = json_obj.1111.name; // Gives me a missing ';' before statement error
In above code, json_obj is a part of a large json file used in our project.
I thought of changing json file to make it easy to locate elements but it is a large json file and is used throughout the project, can someone enlighten me as to how to locate elements in these situations?
In such case you can use bracket notation
var json_obj = {
"1111": {
"name": "Bob",
"id": 1
},
"2222": {
"name": "Alice",
"id": 2
}
}
var first_name = json_obj['1111'].name;
document.write(first_name);

Finding JSON path inside object [duplicate]

This question already has answers here:
Convert string in dot notation to get the object reference [duplicate]
(6 answers)
Closed 8 years ago.
How to check in an JSON object, the input path is present or not?
var obj = {
"schemaOne": {
"name": "abc",
"Path": "i.abc",
"count": 5347,
"subFolders": [
]
},
"schemaTwo": {
"name": "cde",
"Path": "i.cde",
"count": 0,
"subFolders": [
{
"name": "efg",
"Path": "",
"count": 0,
"subFolders": [
]
},
{
"name": "hij",
"Path": "i.hij",
"count": 1,
"subFolders": [
]
}
]
}
}
var inputpath = "obj.count";
After doing several research I came across below code. Here in this code, o.Path is known to the user. But I want to modify the code so tat dynamically check obj.count is present in JSON object or not?
function upd(o, path, count) {
if (o.Path == path) {
o.count = count;
} else {
var arr;
if (Array.isArray(o)) arr = o;
else if (o.subFolders) arr = o.subFolders;
else return;
for(var j=0; j < arr.length; j++) {
upd(arr[j], path, count);
}
}
}
Your question isn't very clear, neither the use case. As someone told u, obj is a JavaScript object, not a JSON.
If I understood correctly, you want to check if a path, espressed as string (such as "obj.count") exists in that object.
A very fast but not safe solution (eval is evil and should be avoided almost in every case) could be:
if(eval(inputpath)){
//... do something
}
In alternative, I suggest you to write a function that gets an object and a string (path) as parameter, parses the string path and check if the object contains such path.
Start from here: Accessing nested JavaScript objects with string key

javascript object variable with # sign [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 8 years ago.
i'm trying to pull some data from last.fm.
i get the following reply:
{
"tracks": {
"track": [
{
"name": "Once Upon a Dream",
"duration": "203",
"loves": "738",
"mbid": "92078817-2e04-4bcd-9c43-ebb9c2d1823c",
"url": "http://www.last.fm/music/Lana+Del+Rey/_/Once+Upon+a+Dream",
"streamable": {
"#text": "0",
"fulltrack": "0"
},
"artist": {
"name": "Lana Del Rey",
"mbid": "b7539c32-53e7-4908-bda3-81449c367da6",
"url": "http://www.last.fm/music/Lana+Del+Rey"
},
"image": [
{
"#text": "http://userserve-ak.last.fm/serve/34s/96432461.png",
"size": "small"
},
{
"#text": "http://userserve-ak.last.fm/serve/64s/96432461.png",
"size": "medium"
},
{
"#text": "http://userserve-ak.last.fm/serve/126/96432461.png",
"size": "large"
},
{
"#text": "http://userserve-ak.last.fm/serve/300x300/96432461.png",
"size": "extralarge"
}
]
}
And so on...
The problem lies when trying to access the image portion of the reply, the image object seems to have #text as variable name of the info i'm trying to access. a normal response.tracks.track[i].image[0].text obviously doesn't work.
is there some special way to access this variable ?
You can use the square bracket notation to access that variable like so:
response.tracks.track[i].image[0]['#text']
It's just a key name inside of the object. You cannot access it via dot notation since it contains an invalid characters but you can use bracket notation. Here's a really simple example demonstrating this.
var foo = {
'#bar': 'http://www.google.com/'
}
foo['#bar'] // will return a string value of http://www.google.com/
If your key contains any invalid characters it must be encased in a string. In your case, you're getting a JSON response which always contain string-encased key names.
Hope this helps!

Categories

Resources