I'm having issues accessing data after fetch [duplicate] - reactjs

This question already has answers here:
How can I use optional chaining with arrays and functions?
(6 answers)
Closed 3 months ago.
I fetched data from an API and I get this :
An array of arrays containing objects and properties containing the details of songs.
The issue is when i try to access the uri property, I get "Cannot read properties of undefined (reading '1')" with the code below :
const songTitleEl = chartData.map(data =>(
<ChartSongs key={data.key} audio={data?.hub?.actions[1]?.uri}/>
))
It should be working perfectly but it isn't and for the life of me I can't figure why.

actions could be null. Need the ? to check if index exists
<ChartSongs key={data.key} audio={data?.hub?.actions?.[1]?.uri}/>

Related

Check if array has same values as other array [duplicate]

This question already has answers here:
Check if every element in one array is in a second array
(10 answers)
Closed 2 years ago.
I am trying to filter my entites by tags array, that can look like this:
const tags = ["tag1", "tag2"];
Every entity has property tags, that can have existing tags, for example:
["tag1", "tag2", "tag3"];
Or:
["tag1", "tag2"];
I need to compare if the tags array and the tags array of entity has the same values.
So far I have tried this code, but it returns an entity even if the two arrays dont have the same values (I'm guessing the includes() function is to blame).
tags.every((tag: any) => doc.tags.includes(tag));
Any ideas how can I check if the arrays have the same values?
Thank you in advance.
You can also compare the length as well
tags.every((tag: any) => doc.tags.includes(tag)) && tags.length === doc.tags.length;

is it ok to add an underline character _ to object keys to prevent the auto-sorting? [duplicate]

This question already has answers here:
Javascript appending object doesn't guarantee order
(3 answers)
Is the underscore prefix for property and method names merely a convention?
(6 answers)
Closed 3 years ago.
I'm using redux and a piece of my state has this structure:
{cityId: {name:cityName, population: numberOfPeople}}
something like:
{
1324:{name:"Clagary", population:1234, id: 1324},
46283: {name:"Edmonton", population: 5678, id: 46283}
}
Everything was fine till i needed to show them in a list with the same order that they have on the main object. At this point, I figured out that objects will auto-sort the keys if they are numbers. I tried to change them to string by putting them inside a "" but it didn't work ({"1324":{...}}).
So i tried adding and underline character as:
({"_1324":{...}})
Now it's working but I just have to double-check with some of you experts here to make sure that this is a good practice and it's normal to do it this way or if there is any better way to deal with numbers as keys in an object when the order matters.
So I'm asking react/redux experts to see if they would move to other solutions when they need the order or would just do what i did.
P.S. I really don't want to go back to use an array as this type of object store is much easier and handy for us in many ways.

Pass array value as parameter to URL in MEAN stack app [duplicate]

This question already has answers here:
Nodejs: url.parse issue with arrays inside query
(2 answers)
Closed 6 years ago.
I'm working in a MEAN app, in which I want to pass an array value to url and fetch values from mongodb. Actually it's a lat and lng. My url should look like:
http://localhost:8080/search?type=latlng&value=[0.123, 0.456]
And have to fetch data related to that lat lng. How to do that?
You cannot pass literal array objects in the URL like you propose. There are workarounds however. You must either separate the data into {key}={value} pairs within the URL, or use a POST request.
GET: http://localhost:8080/search?type=latlng&lat=0.123&lng=0.456
http://localhost:8080/search?type=latlng&coords[]=0.123&coords[]=0.456
POST: http://localhost:8080/search
data: { type: 'latlng', value: [0.123, 0.456] }

AngularJs check weather Array containing an item or not [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 7 years ago.
I have array of name:
$scope.myArray with some values.
Now I want to check an Item is present in array or not,
If it is not present then I want to push that item into array and If that item is already in array then I don't want to push it.
Please tell me how to do this ?
if($scope.myArray.indexOf(item) === -1) {
$scope.myArray.push(item)
}
You can read about indexOf for more details. Also, checkout underscore js for some readily available functions.

CakePHP: $this->Auth->allow() and $this->Auth->allowedActions() - when do I use them? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
AuthComponent: Difference between allowedActions and allow()?
What is the difference between using the method
$this->Auth->allow()
and setting the variable
$this->Auth->allowedActions ?
I can't find any information about setting the allowedActions array where I expected to find it (http://book.cakephp.org/view/1251/Setting-Auth-Component-Variables) - but it exists in the API at http://api13.cakephp.org/class/auth-component.
Can someone please explain which different circumstances I should use them?
See this AuthComponent: Difference between allowedActions and allow()?

Resources