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}))
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:
Create an object from an array of keys and an array of values
(9 answers)
Closed 1 year ago.
consider i have an array say
let arr1=["john","Bruce","Clent"];
and
let arr2=[55,33,22];
Then how can i make an object out of this in javascript
object should look like:{"john":55,"Bruce":33,"Clent":22};
it should take arr1 as object's keys and arr2 as object'values
You can just loop the array, but use the index to match the key and value.
const arr1=["john","Bruce","Clent"];
const arr2=[55,33,22];
const obj = {};
arr1.forEach((val, i) => {
obj[val] = arr2[i];
});
This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 3 years ago.
I am working in react. I have a constant like
const priorityMap = {
"medium":"clean",
"high":"breaker",
"low":"promote"
}
If I do the following , I am getting result.
const map1 = priorityMap["medium"];
console.log("print checkgroup " + map1)
But I want to do the reverse thing. My input is "clean" and I want to retrieve "medium". Is there any way to fetch the key?
It would be :
const valueToSearch = "clean"
const result = Object.entries(priorityMap).find(entry => entry[1] === valueToSearch)[1]
This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
is it possible simplify .map's return statement? I want to add key for each element in the array, and transform arr to array of object. thanks!
let arr = ['c:/a.txt', 'b:/c.txt', 'd:/e.txt'];
let arrObj = arr.map((file) => return {path: file});
You could use a short hand property.
let arr = ['c:/a.txt', 'b:/c.txt', 'd:/e.txt'],
arrObj = arr.map(path => ({ path }));
console.log(arrObj);