This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 6 years ago.
I'm trying to access properties of a JavaScript object by passing a path (string or otherwise):
// In a loop
tableData[i].profile.firstname
where 'profile.firstname' is the path.
Is there a way to access a nested property based on a path in this way?
let firstnamePath = 'profile.firstname'
let firstname = tableData[i][firstnamePath]
Yes, but not with the syntax you've proposed. This is easiest done when your path is an array of strings:
const tableData = (
{ profile: { firstname: 'jim', lastname: 'johnson' }
}
)
const path = [ 'profile', 'firstname' ]
const valueAtPath = path.reduce((_, x) => _[x], tableData)
console.info(valueAtPath)
Related
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 6 months ago.
I'm working with a object like this
userinfo = {
"name":"banana",
"numbers":[1230,991837658]
}
and I need a variable that contains the "numbers" array from the userinfo object, so I should be able to remove a specific value using .splice(var)
let temp = userinfo.numbers
let remove_index = temp.indexOf(991837658)
temp.splice(remove_index,1)
but when I do use the splice method at temp variable I discovered that my variable temp will edit the userinfo.numbers and I don't know why since I tought it was only editing temp
expected:
console.log(userinfo) //output: { name: 'banana', numbers: [ 1230, 991837658 ] }
console.log(temp) //output: [1230]
reality:
console.log(userinfo) //output: { name: 'banana', numbers: [ 1230 ] }
console.log(temp) //output: [1230]
JavaScript objects are passed by reference, so the function changes the initial value. You can remove the reference feature by calling slice with no arguments:
let mySplice = temp.slice().splice(remove_index, 1);
This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 1 year ago.
I have a constant and I want to use it as a key of another object. However, it does not work. What is the best solution for this?
export const URL_QUERY_PARAMS = Object.freeze({
parentId: "parentId",
groupId: "groupId"
})
const queryParam = { {URL_QUERY_PARAMS.groupId : "test1",
"nodePath" : "test2"}
Use array format to make dynamic key
const queryParam = { "nodePath" : "test2" };
queryParam[URL_QUERY_PARAMS.groupId] = "test1";
This question already has answers here:
Find a value in an array of objects in Javascript [duplicate]
(20 answers)
Closed 2 years ago.
I want to make a function to find specific address as (tokia) after that return name. I tried this code but not give me the name only.
let info = [
{name: "ola", address: "tokia"},
{name: "omar", address: "mokla"},
{name: "ohod", address: "gola"}
]
function findNam(info)
{ if(info.address==="tokia")
{ return info.name; } }
console.log(info.find(findNam))
Just access the name property with dot notation. find returns the element that matches the condition, not what you return from the callback.
console.log(info.find(findNam).name)
You can use destructuring to store the name in a variable.
const {name} = info.find(findNam);
console.log(name);
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 3 years ago.
I have this array of object
const d = [{
a_1: 1,
b_2: 2
}]
How can I update a_1 value to 2 without creating a temporary variable?
const myKey = 'a_1'
const myValue = 2
d.map(obj => ({...obj, obj[myKey]:myValue})) //why this won't work?
Remove the obj from obj[myKey] so that [myKey] is correctly seen as a computed property name.
const transformedDs = d.map(obj => ({...obj, [myKey]:myValue}))
This question already has answers here:
Get array of object's keys
(8 answers)
Closed 4 years ago.
Can I get a variable name by code,
receivedData = { id : 1, name : 'adam', notes : ''};
how can I get the variable names in the object "id, name, notes", let's say I want to make a table from the received data and I can't hard code the columns names. is there a way to do this so I can dynamically set the columns names?
columnNames = ['id','name','notes'];
Use Object.keys
let receivedData = {
id: 1,
name: 'adam',
notes: ''
};
console.log(Object.keys(receivedData))