Appending a property to an object in Jquery dot notation [duplicate] - javascript

This question already has answers here:
Use a concatenated (dynamic) string as JavaScript object key? [duplicate]
(6 answers)
Closed 8 years ago.
I am trying to add a property to an object, but Dot notation can't handle a string.
my object:
var lists = {
"Cars":{"Ford":false,"Ferarri":false},
"Names":{"John":true,"Harry":false},
"Homework":{"Maths":true,"Science":false,"History":true,"English":true}
}
Adding a property:
function add_item() {
var input = "Alfa Romeo";
var command = eval('lists.Cars.' + input + '=false');
}
How can I do this using Bracket Notation seeing as it's a 2D object?

No need for eval.. and blah is undefined in your example.
var lists = {
"Cars":{"Ford":false,"Ferarri":false},
"Names":{"John":true,"Harry":false},
"Homework":{"Maths":true,"Science":false,"History":true,"English":true}
}
function add_item(key, value) {
lists.Cars[key] = value;
}
add_item('Alfa Romeo', true);
console.log(lists);

Related

How to update json key passed as a variable with json data in array format in javascript? [duplicate]

This question already has answers here:
How to set object property (of object property of..) given its string name in JavaScript?
(16 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Dynamically set property of nested object
(28 answers)
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 2 years ago.
Below is my json content
{
"demoPO":{
"login":["demoJPLog in", "demoFRLog in","GELog in"],
"cancel":["demoJPCancel", "demoFRcancelo","GEcancelo"],
"content":["demoJPcontent", "demoFRcontent","GEcontent"]
},
"demoPO2":{
"login":["JPLog in", "FRLog in","GELog in"],
"cancel":["JPCancel", "FRcancelo","GEcancelo"],
"content":["JPcontent", "FRcontent","GEcontent"],
"submit":["JPsubmit", "FRsubmit","GEsubmit"]
}
}
I want to update value of key demPO2.login[0]
data.demoPO2.login[0] = value; //this updates key - works
consider user is passing **key** as a variable
var keyName = 'demPO2.login[0]'
data[keyname] = value; //doesn't update, adds a new one
Is there a way to overcome this where user can pass key as variable and update when there are multi-level array in json?
You can use lodash _.set method.
import _ from "lodash";
_.set(data, "demoPO2.login[0]", "test1");
You should extract path to element from key to be ["demPO2", "login", "0"], and then loop it:
var json = {
"demoPO":{
"login":["demoJPLog in", "demoFRLog in","GELog in"],
"cancel":["demoJPCancel", "demoFRcancelo","GEcancelo"],
"content":["demoJPcontent", "demoFRcontent","GEcontent"]
},
"demoPO2":{
"login":["JPLog in", "FRLog in","GELog in"],
"cancel":["JPCancel", "FRcancelo","GEcancelo"],
"content":["JPcontent", "FRcontent","GEcontent"],
"submit":["JPsubmit", "FRsubmit","GEsubmit"]
}
};
function extract(key) {
var keys = key.split('.');
var current = json;
for (var i = 0; i < keys.length; i++) {
if (typeof current[keys[i]] === 'undefined') {
return null;
}
current = current[keys[i]];
}
return current;
}
// For simplicity we assume key is `.` separated
console.log(extract('demoPO2.login.0'));
console.log(extract('demoPO2.content.2'));
console.log(extract('demoPO2.key.notExists'));

How to call function in string on object? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I have:
Main.children.firstfunction.isEnabled = true;
Main.children.second.isEnabled = true;
Main.children.gsdfgsg.isEnabled = true;
Main.children.other.isEnabled = true;
All of these is working good, but such calls is a lot of, so I have in array:
var names = ['firstfunction', 'second', 'gsdfgsg', 'other'];
And I would like do:
for (var name in names) {
Main.children.name.isEnabled = true;
}
But of course it does not work. How can I improve it?
Use bracket ([]) notation. This will allow you to evaluate the property names dynamically.
Try
for (var name in names) {
Main.children[name].isEnabled = true;
}

Using a dynamic variable for the key of an object literal [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I have a series of variables that I would like to pass into an object and I need the left side key to be pulled from a dynamic variable. How would I go about doing this?
Here's an example:
var characteristic = 'color';
var value = 'green';
// Desired JSON output
var object = {
color: 'green'
}
like so:
var characteristic = 'color';
var value = 'green';
var object = {
[characteristic]: value
}
console.log(object);

jQuery expression as object key [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to figure out how to create a dynamic key string for an object. This expression makes JavaScript complain.
return {$(this).val(): true}; // returns an object e.g. {2: true}
What am I doing wrong?
You have to create the object, then use bracket notation for the dynamic key
var obj = {};
var val = $(this).val();
obj[val] = true;
return obj;
or a completely unnecessary one-liner
return (function(o,e) {o[e.value]=true; return o;})({}, this);
The JavaScript object literal syntax {x: y} specifies that x will be a (possibly) quoteless string, and y any value. You can't use this syntax for dynamic keys.
Use this instead:
var foo = {};
foo[$(this).val()] = true;
return foo;

Javascript variable object keys [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
I am attempting to add a variable key, with no luck.
Here's what I got so far:
mysql('translations',{
check: 'element_id',
element_id: element_id,
{'lang-'+lang_id}: value
});
The variable key is the last line of the function.
Any ideas?
You can't use expressions for the identifiers in an object literal.
First create the object, then you can use dynamic names:
var obj = {
check: 'element_id',
element_id: element_id,
}
obj['lang-'+lang_id] = value;
mysql('translations', obj);
You can do this:
var x = {
check: 'element_id',
element_id: element_id
};
x['lang-'+lang_id] = value;
mysql('translations', x);

Categories