How we get data from 1:Array(10) - javascript

Basically I have to list all video which are in 1:Array(10). and I'm unable to get data from this so please help me
Object {1: Array(10), message: "Get all category list", status: "200", result: Array(1)}

If your property is named 1, than you will not be able to do Object.1 . However, you can do this Object["1"]

You have an object as a parameter so just:
const array = obj['1'];
array.forEach( item => {
//do something here
});

Related

Edit readonly javascript objects

I have a read-only object that is returned by GraphQL (vue-apollo) query, the result which is read-only looks something like this:
result: {
id: 'yh383hjjf',
regulations: [{ title: 'Test', approved: false}]
})
I want to bind this to a form and be able to edit/update the values in the regulations array and save it back to the database.
at the moment when I try to edit I get the error below:
Uncaught TypeError: "title" is read-only
I tried cloning the result returned by the database using object.assign
//target template
const regulatoryApprovals = {
id: null,
regulations: [{ title: null, approved: null}]
})
regulatoryApprovals = Object.assign(regulatoryApprovals, result, {
regulations: Object.assign(regulatoryApprovals.regulations, result.regulations)
})
but this didn't work.
Does anyone know how I can properly clone the result?
regulatoryApprovals= Object.assign(regulatoryApprovals, ... indicates the problem because regulatoryApprovals is modified with Object.assign, so it would need no assignment.
Read-only regulatoryApprovals object needs to be cloned. regulations is an array and won't be merged correctly with Object.assign, unless it's known that array elements need to be replaced. It should be:
regulatoryApprovals = {
...regulatoryApprovals,
...result,
regulations: [...regulatoryApprovals.regulations, result.regulations]
}
Where { ...regulatoryApprovals, ... } is a shortcut for Object.assign({}, regulatoryApprovals, ...).

cannot access properties of json objects inside array

I have an array of objects like
[
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
}
]
But when trying to access the object property 'id' using console.log(array[0].id) throws a "cannot read property id of undefined error"
However just logging the first object with console.log(array[0]) prints the object successfully.
{id: 17368, creationDate: 1566802693000, …}
Also printing the list of ids using array.map(x => console.log(x.id)) prints the list of ids successfully .
I am in a situation where i need to access the first few specifically . Where am i going wrong ?
try this console.log(array[0] && array[0].id)
or you can use get from lodash-es like this:
import { get } from 'lodash-es'
const id=get(array[0], 'id', '')

How to access the below json value

Hi I'm trying to access the value DepartmentId but getting error as value doesn't exists.
I can get the values inside the curly braces when I try to access the DepartmentId, I'm getting error.
Below is my map function
r.PrimarySearchResults.map((value) => {
console.log(value)}
)
I can get value.Rank but not value.DepartmentId
Below is the JSON object I got. This is when I expand the Object.
{Rank: "16.9111518859863", DocId:
"17598046715456", Title: "HubSite", SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2",
WebTemplate: "SITEPAGEPUBLISHING", …}
Culture: "en-US"
DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}"
DocId: "17598046715456"
PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7"
Rank: "16.9111518859863"
RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js"
ResultTypeId: "0"
SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2"
SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada"
Title: "HubSite"
UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}"
UrlZone: "0"
WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a"
WebTemplate: "SITEPAGEPUBLISHING"
__proto__: Object
Find the below image
Tried arranging your JSON object, seems that DepartmentId is not included in the JSON
{
Rank: "16.9111518859863",
DocId: "17598046715456",
Title: "HubSite",
SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2",
WebTemplate: "SITEPAGEPUBLISHING", }
Culture: "en-US" DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}" DocId: "17598046715456" PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7" Rank: "16.9111518859863" RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js" ResultTypeId: "0" SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2" SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada" Title: "HubSite" UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}" UrlZone: "0" WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a" WebTemplate: "SITEPAGEPUBLISHING"
Try this:
var value = JSON.parse(value);
console.log(value.DepartmentId);
As mentioned above, the full JSON object being returned should be able to be parsed into an object and then gotten.
If your response object is PrimarySearchResults, you should be able to do:
r.PrimarySearchResults.DepartmentId;
// should return {3d408bfe-9172-4df5-b36e-863c066e9ada}

Read a value from JSON array in Node .js

I need to access the "State" value from the following array --
data =
{
Images:
[
{
ProductCodes: [],
BlockDeviceMappings: [Object],
Tags: [],
ImageId: 'ami-75301c',
ImageLocation: '54696560/Test Image 3',
State: 'available',
VirtualizationType: 'pavirtul',
Hypervisor: 'xen'
}
],
requestId: '2eb809d3-7f82-4142-b5d1-6af3'
}
When I try data.Images["State"] or data.Images.State I get undefined.
Thanks
Images maps to an array which stores objects, so you have to specify the index of the item you want. Try data.images[0]["State"].
You can access like this:
data.Images[0].State
Or even:
data.Images[0]['State']
Access the state with data.image[0].state. Your method was wrong because inside the image, you need an index within the two square bracket, the image property is an array.

Struggling with json array

Ive changed a json object recieved from an api and changed it to an array using $.makeArray and now im struggling to get values from this array. Im wanting temp_c and value within weather desc. A print out of the array is below.
[
Object
data: Object
current_condition: Array[1]
0: Object
cloudcover: "50"
humidity: "72"
observation_time: "10:25 AM"
precipMM: "0.1"
pressure: "1005"
temp_C: "13"
temp_F: "55"
visibility: "10"
weatherCode: "122"
weatherDesc: Array[1]
0: Object
value: "Overcast"
__proto__: Object
length: 1
__proto__: Array[0]
weatherIconUrl: Array[1]
winddir16Point: "SSW"
winddirDegree: "210"
windspeedKmph: "19"
windspeedMiles: "12"
__proto__: Object
length: 1
__proto__: Array[0]
request: Array[1]
__proto__: Object
__proto__: Object
You could try:
alert(yourObject.temp_C);
And
alert(yourObject.weatherDesc.value);
You wouldn't need to convert it to an array :)
You don't need to transform the object into an array, just access the properties you need:
var o = object_retrieved_from_api;
o.temp_c;
o.weatherDesc[0].value;
If you convert it into an array, just have to index first object in array:
var a = $.makeArray(object_retrieved_from_api);
a[0].temp_c;
a[0].weatherDesc[0].value;
The whole point of JSON is that it is already a Javascript object, so you don't need any complex parsing logic in order to get the data out. Try out the following code and you'll see how easy it is to get data from a JSON web service.
$.getJSON('your JSON URL here', function(data) {
$.each(data.current_condition, function() {
alert(this.temp_C + "C " + this.weatherDesc[0].value);
});
});

Categories