How can I replace last part in structure this.props.{myName} - javascript

I have name that I get from component(for example "popular" and I get it by "this.props.name") and I need to push that name in this structure: "this.props.{name which I get} => this.props.popular", but I don't know how I can do this.
I tried do this:
let needName = this.props.`${this.props.name}`;
But it didn't work. I know that it's strange question but I need to solve it by this way.

If you're trying to use a variable to dynamically create the reference to a property in an object, you can use bracket notation:
let needName = this.props[this.props.name];
For more information, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties

Related

Access Array with String key

I have two variables with JSON files. The first is a list of keys looks like this:
keylist = ["key1","key2","key3"]
The second one is generated from a database and looks like this:
data = {
"key1"{
#further data
},
"key2"{
#further data
},
"key3"{
#further data
}
}
Now I want to access the second element of the database with the key from the keylist
data.keylist[1];
Which doesn't work because the return of keylist[1] is a String? I did some research and the use of the window function was proposed. So I tried this:
window["data." + keylist[1]]();
Which leads to a "is not a function" error. What can I do to solve this problem?
As simple as that:
const mydata = data[ keylist[1] ];
Also, your code is correct from the point of syntax, but it tells completely different than you expect it to tell.
data.keylist[1];
tells JS that you expect to have an object called data which has a property called keylist and which is (most likely) type of array, and you want to get the second element of this array.
PS: And one more point here. Your question title is not completely correct because of the difference between Arrays and Object in JS.
There is no "string keys" for arrays in JS, so you cannot "access array with a string key". Well, truly speaking there are, but not for items of array. Array items only have numeric index, which you can use to access it. Objects, in contrast to arrays, may have named properties. So when you see something like that: data = myVar['data'], you can tell that you're dealing with an object, while data = someVar[0] can be both, an Array (most likely) or also an Object with key named '0'.
I don't think the issue you're having with your first example is because it returns a key. I believe the issue is because data doesn't have a property called keylist. Instead of that, try it as
data[keylist[1]]
and see if that works for you. The reason this one should work is that, in this situation, Javascript will evaluate the string return of keylist[1] and then use it as a string index for the data variable. Let me know if this works out for you :D
You can try using using something like this.
data[keylist[1]]

Get value in data object Javascript

I have a data object, and i want to get on of the value from it, when i try to print the data:
console.log(data);
i got an object like the image below :
the problem is i want to get the order[billing_address][country_id] which i think is an object, but i don't know how to fetch it. i've tried :
console.log(data.order); //didn't work
console.log(data.order[billing_address][country_id]);//didn't work
The name of the property is: "order[billing_address][country_id]"
To access its value try:
console.log(data['order[billing_address][country_id]'); // Should work
It appears that the values you are looking for have keys that are the whole string:
"order[billing_address][telephone]"
You can access these values like this:
data["order[billing_address][telephone]"] //"5"
You are currently trying this:
data.order[billing_address][country_id]
What you are trying doesn't work because there are no variables billing_address or country_id that are defined, and the object is not that deeply nested - just has the above mentioned long string for a key.

Calling array names dynamically inside jQuery

I'm pretty sure this question is going to get negative response as many people have already asked it in SO. But trust me I have read every single answer of every single question none helped.
I know that in jquery you can put the array key name dynamically like this: somearray1["abc" + variable] but I'm not looking for that. I want to call the array name dynamically like:
var i=1;
console.log( "somearray" + i["abc" + variable] )
Can someone tell me how is it possible? I cannot put it in another array and call that as I'm building a very dynamic script, so I must need to call the array name dynamically.
Any help will be highly appreciated.
Normaly, your array depend from this.
this["somearray" + i]["abc" + variable]
var bob1 = [1,2,3];
var name = "bob";
console.log(this[name+"1"][0])
I'm not sure exactly what you're asking here from your example. Do you want to console.log the value held at a dynamically changing position of "somearray"? If so, array positions are numerical indices (e.g. somearray[1]) and cannot be accessed via strings.
Are you trying to access an object property (e.g. someObject1["abc" + variable]) alternatively?
If it's an object property you are trying to access with changing parameters, you might want to try using ES2015 template literal syntax.
let i = 1;
let someObjectName = `someObject${i}`;
console.log( someObjectName[`abc${variable}`] );
That way the concatenation of the object name and its property will happen dynamically. I hope this helps.

Using a dynamically created variable to access named object in an object

finalVariables() returns an object that contains data accessible by .dataName notation i.e. finalVariables().mainViewWindow0 returns the string stored for mainViewWindow0. I'm trying to access mainViewWindow0 using a dynamically created variable, but for obvious reasons this doesn't work so well with dot notation, but I don't know how to work around it. Help to be had for me?
Please ignore the poor coding practice of having a hard-coded number in there; I promise to get rid of it later
activePane = dot.id.substring(6); //gets dot # and sets it as active pane
var tempForPaneNumber = "mainViewWindow" + activePane + "";
document.getElementById("mainViewWindowContent").innerHTML = finalVariables().###this is where I want to use
the string from "tempForPaneNumber" to access ###
finalVariables[tempForPainNumber]()
Should do the trick if I understand correctly.
In Javascript you can access properties of an object either through the dot notation or through the use of brackets to specify the identifier for the property so myVar.foo is equivalent to myVar['foo']. Therefore, if I understand what you are asking correctly you want to use finalVariables()[tempForPaneNumber]()

Using a string to reference an object name

I'm trying to replicate a "feature" of PHP (And in most languages) in Javascript.
Here it is in PHP:
$objectName = 'ObjectA';
$someObject->$objectName->someMethod();
Basically using a string variable to reference an object variable.
So in Javascript, I was hoping to do something like:
var objectName = "ObjectA";
someObject.[objectName].someMethod();
Anyone know how to do this? Or if its even possible?
You almost have it, just remove the first ., like this:
var objectName = "ObjectA";
someObject[objectName].someMethod();
If you want to search for more info around this, it's called bracket notation.

Categories