String to access nested value from object in array [duplicate] - javascript

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 3 years ago.
I have a somewhat strange question,
I would like to know if it is possible to use a string as an entire reference to get a value from an object within an array.
This is my array:
const myArray = [
{name: 'element1', id: 'elementid1'},
{name: 'element2', id: 'elementid2'}
];
where myArray[0]["name"] returns: 'element1'
Would it be possible to have this entire reference: myArray[0]["name"] as a string: 'myArray[0]["name"]' and use it to reference to this value.
So this: getViaString returns 'element1' with the following set up:
const getViaString = 'myArray[0]["name"]';
I have set up this fiddle as it probably explains better what I am trying to do:
jsfiddle
Thanks.

You could potentially use eval() - not recommended.
const getViaString = eval("myArray[0]['name']");

yes you can do it with
const getViaString = eval('myArray[0]["name"]');

Related

How to change the value of an object inside an array - Javascript [duplicate]

This question already has answers here:
array indexOf with objects?
(4 answers)
Closed 2 years ago.
const nonsense = [{x: '1'}];
console.log(nonsense.indexOf({x: '1'})); // prints -1
I don't understand why it's printing -1 even though the specified object clearly exists in the array.
objects are compared by their reference, not by their values.
object inside nonsense array is a different object from the object you passed to indexOf function even though they have same key-value pairs.
Following code will give you the correct index
const obj = {x: '1'};
const nonsense = [obj];
console.log(nonsense.indexOf(obj));
As, in javaScript the objects are compared by reference, not by the values.
const nonsense = [{x: '1'}];
console.log(nonsense.map(o => o.x).indexOf('1'));
or, probably with better performance for larger arrays:
console.log(nonsense.findIndex(o => o.x ==='1'));

get value of an array's items within an object using the key [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 3 years ago.
i need some solutions.
Here is my situation; i want to get the value of an array's item within an object using the "KEY". For example, i have this object:
const obj = {
id: 1,
fields: [
{id: 1, name: 'test'},
{id: 2, name: 'test2'},
]
}
and i want to get name's value of the first element in fields. So the solution i know is that we can do: obj['fields'][0]['name'] ... but what i'm looking for is just doing something like obj[KEY].
So the question is: is that possible and what kind of KEY i can use to do it ?
This is not possible. You are trying to access a nested value (the property of an array item, inside the property of an object).
Instead, you'll have to be explicit in how you access this field:
obj.fields[0].name.
What are you trying to achieve and why do you want to use a single key?

How to access value in Object? [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 3 years ago.
I have this Array Object that looks like this:
let arr = [
{Name: "sub", Value: "ababbnnn"}
]
I'm trying to access the value of the Name custom:network meaning I want to output this: abcdef1233bfgh. So far I have this loop but I wonder if there may be a cleaner way. Thanks a lot in advance. Here's my code:
You can use the find method:
const value = arr.find(item => item.Name === "custom:network").Value
To cover the case where no item is returned by find, you can use the following approach:
const value = (arr.find(item => item.Name === "custom:network") || {}).Value

Writing key and value in json [duplicate]

This question already has answers here:
What is the difference between object keys with quotes and without quotes?
(5 answers)
Closed 5 years ago.
I am very new to javascript. We can write json as follows
const obj1 = {key: "myKey", value: "myValue"}
const obj2 = {"myKey": "myValue"}
what is a difference between these two?
And when should I prefer using one over another?
The first one is the JavaScript way. The second one would be JSON format. You can use both in JavaScript. My preference is the first way.
As far as I know, it doesn't matter. It's up to you.
There isn't a difference. Write however you want. I personally prefer the key not to be a string.var a = {key: "value"}

Self assign properties in an object? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 8 years ago.
I am trying to initialize an object and and assign a property of its own to one of the properties.
But my syntax is incorrect.i am referring to the following line:
PCMACUrl = genericURL + "/test"
i have tried
testList[0] = {
executionTimeSec:60,
genericURL:"www.gmail.com",
comments: "",
PCMACUrl = genericURL + "/test"
};
After re-reading all together I believe this is what you are looking for:
(added after init, yes, I know, but it's clean and simple :)
var data = {
'PropA': 1,
'PropB': 2,
'PropC': 3
};
data.PropD = data.PropC+5;
console.log(data); //Object {PropA: 1, PropB: 2, PropC: 3, PropD: 8}
Or, another way to look at it:
if it is possible use backend to generate the object you would like
probably you could also just get rid of same data in same object and use it differently when you call it (referencing to first object propertie and adding second on-the-go:
.../ = data.PropC+anotherData.PropA /...

Categories