I have a javascript object literal?
I access them in my webpage as
data.list[i].ger or data.list[i].eng
If I want to directly search an entry how can I directly access that entry?
or do I have to do a linear search or a binary search upon sort?
data = {
list: [
{
"ger": "A-as",
"eng": "A as"
},
{
"ger": "A-aws",
"eng": "a-was "
},
{
"ger": "we",
"eng": "cv"
},
{
"ger": "q",
"eng": "w-la-w"
},....
for e.g. if i wanted to access
"ger": "q","eng": "w-la-w"
I would acces it as data.list[4].ger
is there any way of directly accessing knowing what is the index of that entry?
I think you may be looking for JSONSelect. It makes it easy to access data in complex JSON documents, but looks like CSS.
Check out the examples, it looks pretty much like what you want to do.
For instance, to get the eng version of ger:q, you would do .ger:val("q") ~ .eng
You can download it here and use it with JSONSelect.match or JSONSelect.forEach.
If you just want to search through your array, I'd go with something like
function lookupTrans(value, fromLang, toLang) {
for (var i = 0, dataLen = data.list.length; i < dataLen; i++) {
if (data.list[i][fromLang] === value) {
return data.list[i][toLang];
// return i (if you're lookup for just the index number)
}
}
return "";
}
However, I do want to make sure you're not trying to just do one-way internationalization. If that's the case, I'd recommend just using named keys:
var i18n = {
"translation": {
"en": "translation",
"ja": "hon'yaku"
},
"list": {
"en": "list",
"ja": "risuto"
},
"example": {
"en": "example",
"ja": "rei"
},
"imbueWithConfidence": {
"en": "Let's translate!",
"ja": "Hon'yaku shimashou!"
}
};
Related
I am having trouble trying to present an array of objects on the tooltip of an Anychart.js map. I understand that we can access the dataset by doing something like: %[name of property in data set]. My data set has the following form:
{
"country": "Austria",
"id": "AT",
"continent": "Europe",
"songs": [
{
"rank": 33,
"title": "Stuck with U (with Justin Bieber)",
"artists": "Ariana Grande, Justin Bieber",
"album": "Stuck with U",
"explicit": 0,
"duration": "3:48"},
{
"rank": 34,
"title": "Late Night",
"artists": "Luciano",
"album": "Late Night",
"explicit": 0,
"duration": "3:20"
},
... more objects
]
}
}
If I wanted to access the Country property I would simply add it to the tooltip by doing:
tooltip.format("Country: " + {%country});
The issue is when trying to access an array of objects, I have tried different variations and none of them worked. Trying to show the title of every song:
tooltip.format({%songs}.{%title});
tooltip.format({%songs.%title});
tooltip.format({%songs}[{%title}]);
I also saw in the documentation that we can send a function as argument so I tried the following where I would concatenate every title of the collection but did not succeed either:
tooltip.format(function() {
let concatenated = '';
this.songs.forEach(song => {
concatenated += song + ' ';
});
return concatenated;
});
I would really appreciate your help guys.
String tokens do not support nested objects/properties. But you can use the callback function of the formatted to get access to songs. The context prototype includes getData() method provides that. Like this:
series.tooltip().format(function() {
console.log(this.getData('songs'));
return 'tooltip';
});
For details, check the live sample we prepared.
In case any one else is looking for a solution to this answer. I figured out how to loop through an embed array, and call on specific information.
chart.edges().tooltip().format(function () {
var format = ''
var songs = this.getData('songs');
songs.forEach(function (data, builtin, dom) {
format = '<p>'+data['title']+' by '+data['artists']+' </span></p>' + format
});
console.log(format)
return format
});
I'm currently using the fixture file to make sure it will be easier to call the right value.
cy.fixture('latestLead.json').then(function (lead) {
this.lead = lead
})
My son file is the following:
{
"status": 0,
"result": {
"totalSize": 1,
"done": true,
"records": [
{
"attributes": {
"type": "Lead",
"url": "/services/data/v51.0/sobjects/Lead/111111111"
},
"Id": "111111111",
"Name": "Andres Latest Test"
}
]
}
}
The way that I'm trying to get the right value is the following:
cy.get(".gLFyf").type(this.lead.result.records.Id)
I'm able to get totalSize or done from the result object, but I'm not able to get any other value higher than that object. How can I get the Id value from the records Array?
You can access an array item (in your case it's the object) using the index position (in your case it's zero)
cy.get(".gLFyf").type(this.lead.result.records[0].Id)
try this
cy.get(".gLFyf").type(this.lead.result.records[0].Id)
I want to get the req1 value in the JSON below, programatically.Here RequestTypeItem can be changed as well, so it is not fixed. Else I could have navigated it using object.subobject
I was able to navigate till slots using
var b = JSON.parse("{ .... }");
b.request.intent.slots.RequestTypeItem.value
But I can navigate further programatically.
{"request": {
"locale": "en-US",
"timestamp": "2016-09-25T00:36:14Z",
"type": {
"name": "request",
"slots": {
"RequestTypeItem": {
"name": "RequestTypeItem",
"value": "req1"
}
}
}
}
}
In your JSON your request does not have a property of intent, it does have a property type, so you then you can access the property you want with
b.request.type.slots.RequestTypeItem.value
JSFiddle: https://jsfiddle.net/9cexbn54/
Edit: After reading your question again, maybe this is what you want:
// loop through all properties on the slots object
for (var i in b.request.type.slots) {
if (b.request.type.slots.hasOwnProperty(i)) { // make sure it is a property belonging directly to slots, and not "inherited" from the prototype chain
if (b.request.type.slots[i].value) { // make sure that the sub-property of slots has a value property
document.getElementById("output").innerHTML = b.request.type.slots[i].value;
break; // break out of the loop after getting a value
}
}
}
Here I loop through all the properties on slots, checking that the property does indeed belong to slots, and that it has value property.
JSFiddle: https://jsfiddle.net/9cexbn54/1/
I had given a link which some of you found it not useful.
Here is what would get to req1:
$data=#'
[{"request": {
"locale": "en-US",
"timestamp": "2016-09-25T00:36:14Z",
"type": {
"name": "request",
"slots": {
"RequestTypeItem": {
"name": "RequestTypeItem",
"value": "req1"
}
}
}
}
}]
'#
$json=ConvertFrom-Json $data
$json.request.type.slots.RequestTypeItem.value
I really want to convert a object to array but my codes doesn’t worked.
data = "errors": {
"user": {
"name": "empty"
},
{
"length": "exceeds"
},
"title": {
"name": "empty"
},
{
"length": "exceeds"
}
}
Now I want to make them:
data = ["empty", "exceeds", "empty", "exceeds"];
What I’ve done so far is:
var arr = Object.keys(data[i].data.errors).map(function(k) {
return data[i].data.errors[k]
});
console.log(arr);
But the output is not what I expected. Please help. Thank very much.
If you always know the keys of the inner objects are going to be name and length a short way might be:
var out = Object.keys(data.errors).reduce(function (p, c) {
return p.concat([data.errors[c].name, data.errors[c].length]);
}, []);
DEMO
Let's say I have the next JSON file:
{
"shows": [
{
"name": "House of cards",
"rating": 8
},
{
"name": "Breaking bad",
"rating": 10
}
]
}
I want to access the rating of a show, by it's name. Something like this:
var rating = data.shows["House of cards"].rating;
Is this possible? Or something similar?
Thanks a lot!
You won't have such hash-style access just by deserializing that JSON sample.
Maybe you might be able to re-formulate how the data is serialized into JSON and use object literals even for shows:
{
"shows": {
"House of cards": {
"rating": 8
}
}
}
And you can still obtain an array of show keys using Object.keys(...):
Object.keys(x.shows);
Or you can even change the structure once you deserialize that JSON:
var x = { shows: {} };
for(var index in some.shows) {
x.shows[some.shows[index].name] = { rating: some.shows[index].rating };
}
// Accessing a show
var rating = x.shows["House of cards"].rating;
I suggest you that it should be better to do this conversion and gain the benefit of accessing your shows using plain JavaScript, rather than having to iterate the whole show array to find one.
When you use object literals, you're accessing properties like a dictionary/hash table, which makes no use of any search function behind the scenes.
Update
OP has concerns about how to iterate shows once it's an associative array/object instead of regular array:
Object.keys(shows).forEach(function(showTitle) {
// Do stuff here for each iteration
});
Or...
for(var showTitle in shows) {
// Do stuff here for each iteration
}
Update 2
Here's a working sample on jsFiddle: http://jsfiddle.net/dst4U/
Try
var rating = {
"shows": [
{
"name": "House of cards",
"rating": 8
},
{
"name": "Breaking bad",
"rating": 10
}
]
};
rating.shows.forEach(findsearchkey);
function findsearchkey(element, index, array) {
if( element.name == 'House of cards' ) {
console.log( array[index].rating );
}
}
Fiddle
var data = {"shows": [{"name": "House of cards","rating": 8},{"name": "Breaking bad","rating": 10}]};
var shows = data.shows;
var showOfRatingToBeFound = "House of cards";
for(var a in shows){
if(shows[a].name == showOfRatingToBeFound){
alert("Rating Of "+ showOfRatingToBeFound+ " is " +shows[a].rating);
}
}