How to concatenate variable names in objects [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I am checking a value from an input text a form, the name of the input is
name="name['title']"
title, doing something like this:
if (!values.title) {
errors.title = 'Required';
}
I have to do this for some other controls, so I thought I could make the control name dynamic, how can I achieve something like this?
let name = 'title';
if (!values.<name>) {
errors.<name> = 'Required';
}
I updated the question to show how it is different from proposed solutions.

Related

How to replace .Id with a variable string value [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
I currently have the following in my Javascript code where Id is a field of an object:
this.recId = event.detail.row.Id;
I want to make the Id part generic something like
String a = 'Id';
this.recId = event.detail.row.a;
How do I achieve this in Javascipt
Use [ ] instead.
const obj = {
id: "myObject"
}
const a = "id"
console.log(obj[a]);
In your example, it would be
this.recId = event.detail.row[a];
You can use
this.recId = event.detail.row[a] to get the desired result

How to use variable to dynamically change object attribute? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
let property = $(this).val();
let property_name = this.dataset.property;
headObject[selected].property_name = property;
});
property_name is a variable not key. Are there anyway to handle this? Thank you in advance!
I can see you are uing jQuery.
headObject[selected].attr( property_name, property ); //Where headObject[selected] is a jQuery object

Javascript - Set array member using variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
I need to be able to set the member of an array using a variable, catch being that the variable defines the name of another arrays member. I'm writing in Typescript and ES6.
The code is passed an array as a variable e but the name of the text: key will be e.[variable].
What is the correct format for setting that variable member?
options.push(
{ key: e.Id, text: e.[variable] }
);

How to add/remove properties to object in runtime in javascript [duplicate]

This question already has answers here:
Adding elements to object
(19 answers)
Closed 6 years ago.
I would like to know how to add/ remove properties to object at run time in javascript? How to achieve this in javascript ?
Let's say your object is myobj
then you can add a member like this
myobj.myvar = value; or myobj["myvar"] = value;
and remove it with
delete myobj.myvar; or delete myobj["myvar"];

How to return value to the parent form [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have a popup form and need to return a value to the master form. The first statement works fine, but when I try to put the field name (textAlert) to a variable, I cannot get it to work. I tried all kinds of syntax.
Please help. Thanks.
function DoReturnValue()
{
opener.MasterForm.textAlert.value = "Hello"; // WORKS
/* DOES NOT WORK
var theField = "textAlert";
opener.MasterForm(theField).value = "Haloha";
opener.document.MasterForm.getElementById(theField).value = "Haloha";
*/
}
To access properties dynamically with a string variable as a name, use square-bracket notation:
opener.MasterForm[theField].value = "Haloha";

Categories

Resources