Accessing dynamic content with numbers [duplicate] - javascript

This question already has answers here:
read name of unknown properties
(2 answers)
Closed 8 years ago.
I'm working with wikipedia API and have a problem with the results I'm getting.
{"query":{
"pages":{
"48636":{
"pageid":48636,
I don't know what the ID is going to be when I make the call, how do I access the 48636?
if gon.activateWiki
$(gon.keywords).each (index, element) ->
console.log element.name
$.getJSON( 'http://sv.wikipedia.org/w/api.php?action=query&format=json&prop=revisions%7Clinks%7Cimages%7Ccategories&rvprop=content&titles=' + element.name + '&callback=?' , (data) ->
console.log data.query.pages
)

You want the trusty old Object.keys
firstKey = Object.keys(data.query.pages).shift()
lastKey = Object.keys(data.query.pages).pop()
nthKey = Object.keys(data.query.pages)[n-1]
firstPage = data.query.pages[firstKey]
lastPage = data.query.pages[lastKey]
nthPage = data.query.pages[n-1]

You can access it like an array:
console.log(data.query.pages["48636"]);
Update:
To get values from unknown property's of object you need to use for..in statement. Or you can use slice method and transform your object to array:
var pages = Array.prototype.slice.call(data.query.pages);

Related

how to select an index that has a specific name without case sensitivity? [duplicate]

This question already has answers here:
Access JavaScript property case-insensitively?
(19 answers)
Closed 2 years ago.
and I want to choose the index where the table is case insensitive
let table = {'tAble': [{'number': 1}],'CHair': [{'number': 2}]}
console.log(table[/table/i])
You can do this using Object.keys
table[Object.keys(table).find(x=> /table/i.test(x))]
Here you're getting an array of all keys, and then using find to test them with your RegExp, and then using the found key to retrieve the correct property of the object.
let index = Object.keys(table).findIndex(key => key.toLowerCase() === 'table');
let table = {'tAble': [{'number': 1}],'CHair': [{'number': 2}]}
let key = (Object.keys(table).find(i=>i.toLowerCase() === 'table'));
console.log(key)
console.log(table[key])

how to findIndex of [duplicate]

This question already has answers here:
How to find the array index with a value?
(12 answers)
Closed 5 years ago.
I'm wondering how to get the findindex of a specific service_name in the below array?
var obj = {"ID":111222,"NAME":"Chicken","FREQUENCY":"Yearly","service_name":["Open ticket","Service Account time","Assets Management Overview"]}
I have tried the following but not getting the correct index
var find_index = obj.service_name.findIndex(function(obj){return obj.service_name = "Open ticket"})
console.log ("The index of " + find_index);
Your code is close, but there are a couple of semantic issues. You're already iterating over the service_name array so the argument that the .findIndex callback takes is the array element / string itself. You also need to use == or === for comparison. Right now you are performing assignment.
const find_index = obj.service_name.findIndex(
serviceName => "Open ticket" === serviceName
);
In this case you can also use .indexOf which would be a bit simpler:
const index = obj.service_name.indexOf("Open ticket");

How to get property value from javascript object which has dynamic property names? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have a query created dynamically like,
"$select=" + selectedField + "&$orderby=" + selectedField + ""
This is just the dynamic part of the query where 'selectedField' is a string.
Now when I am getting my results (array of objects) and I am trying to get the values within a loop like,
for(var i=0; i<results.length; i++) {
var opt = results[i].selectedField;
}
opt is undefined. However, if the value of selectedField is 'new_name' and I write
var opt = results[i].new_name;
I get the desired result. Can anyone help me to resolve this?
Use var opt = results[i][selectedField];.

Assign dynamic index in array of object in Angularjs [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
If I have array of object in such a way.
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
Using above I want to assign Prashant in $scope.getColumn[i].value in dynamic way. I have try like this
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
but this give me Uncaught SyntaxError: Unexpected token +
I can get Prashant Shukla by data[0].name but How can I get exactly same by using $scope.getColumn[i].Field in place of name index ?
how can I resolve this error and assign value dynamically?
thanks ahead.
you can use as like as given below:
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
console.log( data[0][ $scope.getColumn[i].Field ] ); // 'Prashant Shukla'
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
Why you have . after the data[0]?
It should be data[0].name +$scope.getColumn[i].Field;

How to access object in jquery [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
var v = [{"filename":"1.mp4","resname":"1280x720","videolength":"00:00:26.07"},{"filename":"2.mp4","resname":"854x480","videolength":"00:00:26.07"},{"filename":"3.mp4","resname":"640x360","videolength":"00:00:26.07"}];
I am using $.parseJSON(v); but have error , any suggestion to get the value of resname ?
Try:
var v = [{"filename":"1.mp4","resname":"1280x720","videolength":"00:00:26.07"},{"filename":"2.mp4","resname":"854x480","videolength":"00:00:26.07"},{"filename":"3.mp4","resname":"640x360","videolength":"00:00:26.07"}];
$.each(v,function (i, val) {
console.log(val.resname)
});
There is no need to parse v you can access it in different ways
one is by iteration or forloop
v.forEach(function (item, i) {
alert(item.resname)
});
and you can also directly access the elements by using
alert(v[0].filename);
alert(v[1].filename);

Categories

Resources