How to use a string value inside a javascript object path - javascript

I have extracted the following from a string and assigned it to arrPath , i.e.:
arrPath = "myArray[0]";
I also have an object path that points to a value but I need to take the value of arrPath and use this in my object path, i.e.
myPath.sheet.value.arrPath.info.status
Unfortunately this is not working for me as I basically need the path to look like the following:
myPath.sheet.value.myArray[0].info.status
in order to access the value.
I have had a look at other potential solutions but can't seem to locate one that will assist with my question.

Related

React how to address a key from string name

Ok, so I have a database with data sets in it. the application performs a base API call to retrieve that base data set containing all other data sets. I will receive a string variable with the name of the key I need to access so let's say const addon = "Book". However, I don't know the key name beforehand. So the following code works but I need to somehow not hard code the key parameter but rather use the string value incoming from the const addon. I am not quite sure how to do this please point me to the right documentation or explain how to achieve the wanted result.
const columns = levelOne.Book && Object.keys(levelOne.Book);
However, as the incoming param might not be "Book" but anything else it will only work for this case. It is guaranteed that there is a key-value pair where the key bears the name stored in the string value of addon.
You can use a variable as the key. For example, levelOne[variable] where variable is the string that you want to use as the key.
Also, you can get the keys through Object.keys(levelOne) and then you can set variable value from the keys array.

Adding values to object in js

So, I have the following js object structure:
I am trying to add a value to this, for example, "234324" after "ewerewr".
I tried obj["d7vi"] = new_value;
But it gets rid of all the previous values.
Any help?
make it
obj["d7vi"].push(newValue);
you need to add to the array rather than replace its existing values.

jQuery Id Selection and Dynamic Arrays

I have some code I'm struggling with. The good news is the code working as intended for a single instance; after some thought I've decided to feature multiple of these image selectors on a page. This works but the ugly approach of duplicating the code doesn't scale well (e.g. what if you want 50 of these on there?) The snag I've hit is how I can refer to a specific array. Is an array even an ideal solution for this?
The Objective
I have a series of images that a user may select from, up to X amount. The selected image ids are stored in an array and the image is added to a "selected images pool". This occurs by using an onClick for the slider, I obtain the Id from the element attributes. This is where I'm getting stuck.
var dataArray = $(this).closest("[id^=carousel]").data('array');
var slideCounter = $(this).closest("[id^=carousel]").data('counter');
slideCounter = dataArray.length;
The slideCounter returns the length of the string, not the array elements. How can I tell this code to refer to a particular array? See the fiddle for a better idea of the markup and code: jsFiddle
I have no doubt that there is a better approach. I'm relatively new to front end work, I'd appreciate any insights, I've burnt some brain cells on this, thanks!
From looking at your HTML, it looks like when you do this:
var dataArray = $(this).closest("[id^=carousel]").data('array');
what you're trying to do is to read the name of an array with .data() and then somehow turn that name (which is a string) into the array that's in your variable. My guess is that there's probably a better way to structure your code rather than putting javascript variable names in your HTML. I'd probably put a key name in the HTML and then store the arrays in an object where you can access them by that key name at any time.
Without trying to refactor your code, here's an idea for what you were trying to accomplish:
If selectedSlidesIdArray1 is a global variable, then you can do this:
var dataArray = window[$(this).closest("[id^=carousel]").data('array')];
Using the [stringVariable] notation on an object, lets you access a property by a literal string or a variable that contains a string. Since all global variables are also properties on the window object, you can do it this way for global variables.
If selectedSlidesIdArray1 is not a global variable, then you should probably put it in an object and then you can do this:
var dataArray = yourObj[$(this).closest("[id^=carousel]").data('array')];
Instead of trying to translate an arbitrary string into a JavaScript variable of the same name, why not just use another array? You can have nested arrays, which is to say an array of arrays.
Thus, instead of selectedSlidesIdArray1, selectedSlidesIdArray2, etc., you would have one selectedSlidesIdArray with sub-arrays, which you could then pull the index for using a data attribute.

How to assign values to objects through a variable that is just a reference to them?

I couldn't really word the question less vaguely, but I think you will understand...
I am developing a game engine in Javascript, and the Scene object, which is a container of many things, has a method that is supposed to change one array in it, specifically the one holding all the things that can be drawn.
This array is accessed like this:
scene.internals.draw
The problem is, it is referenced many times in the method, and I think that the name/path might change. Naturally, I don't want to change every reference to it in the method each time I change the the array's path, so I did this:
var location = scene.internals.draw;
Now, the actual method code and the algorithm can stay intact, and if the name/path of the array in the scene changes, I only need to change that one line.
And it works pretty well for the most part. I can .push(obj) to it, etc, but at one point, I need to "disect" the array, ie, split it in half, add something, and then put it back together, like this:
buff1 = location.slice(0, i); //First slice of the array.
buff2 = location.slice(i, location.length); //Second slice of the array.
//Add something in between those slices.
buff1.push(ob);
location = buff1.concat(buff2); //Problems here!
This worked well while location was just scene.internals.draw, as it changed the array directly. But now, I assign the new value to the local location variable, not the desired scene.internals.draw one!
Question: how can I, using the = operator, assign values to "real" objects, instead of the variables that contain references to these objects (like location)?
The obvious solution would be this, at the end of the method:
scene.internals.draw = location.slice();
This is OK, the only side effect is that I will have to write the original name twice, and edit it twice, which isn't such a big issue. But, I maybe find myself in other situations where I just might need that functionality, so I'd still like an answer.
There is no assignment by reference in javascript, so you cannot do this. What you are doing is usually mistaken for assignment by reference but it is in fact a copy of a reference value which has implications like this.
You probably have a deeper problem somewhere since you are doing this but I don't wanna get into that.
You could do this:
location.splice( 0, location.length ); //Remove all items in the array
location.push.apply( location, buff1.concat(buff2) ); //Push the buffers into the array
To use your term, there are no "real" objects in Javascript - there are only objects, and the variables that hold references to them.
When you assign to location you're just creating an additional reference to an object. The system has no knowledge of which "real" object it was, nor of any other variables that may hold references to it.
So when you reassign to location you're just overwriting that particular reference. Any other original references to the object will stay pointing just where they were.

How to use a function parameter to refer to the variable of the same name?

I'm making a jquery plugin to show piano scales.
I have a function called markScale(a, b) which will be used to highlight certain scales (the a parameter gives the pitch of the starting note, ie. how many semitones up from C, the default scale). That's no problem.
The problem comes with b, the type of scale or chord to display. I have defined which keys to use in the different types of scale as follows:
var majorScale=[12,14,16,17,19,21,23,24];
var nminorScale=[12,14,15,17,19,20,22,24];
var hminorScale=[12,14,15,17,19,20,23,24];
And so what I'm looking to do is the following:
for(i=0;i<8;i++){
$('#key-'+(b[i]+a)+'-marker').show();
}
markScale(0,"majorScale") doesn't work, because that's just a string and doesn't refer to the array variable that I need.
How do I refer to the array variable as a parameter of the function?
Thanks
I think we're missing a little info, but if you want to asccess the scale by name, put the arrays in an object instead of individual variables.
var scales = {
majorScale:[12,14,16,17,19,21,23,24],
nminorScale:[12,14,15,17,19,20,22,24],
hminorScale:[12,14,15,17,19,20,23,24]
};
Then you can reference the scales using a string...
var the_scale = "majorScale"
scales[the_scale][i];
While it is possible to refer to local variables from a string, it requires an approach that is generally not recommended. Global variables are a little easier.
If you were trying to pass the scale, then you don't use a string at all. You just use a direct reference. majorScale
Its hard to tell without more context, but have you tried passing the array majorScale itself to the markScale() function?
as in:
markScale(c, majorScale);
instead of a string:
markScale(c, "majorScale");

Categories

Resources