我使用jQuery从DOM中提取项目,并希望使用idDOM元素在对象上设置属性.
const obj = {}
jQuery(itemsFromDom).each(function() {
const element = jQuery(this)
const name = element.attr('id')
const value = element.attr('value')
// Here is the problem
obj.name = value
})
Run Code Online (Sandbox Code Playgroud)
如果itemsFromDom包含一个带有id"myId" 的元素,我想要obj一个名为"myId"的属性.以上给了我name.
如何使用JavaScript使用变量命名对象的属性?
用文字解释这个案子很难,让我举一个例子:
var myObj = {
'name': 'Umut',
'age' : 34
};
var prop = 'name';
var value = 'Onur';
myObj[name] = value; // This does not work
eval('myObj.' + name) = value; //Bad coding ;)
Run Code Online (Sandbox Code Playgroud)
如何在JavaScript对象中设置具有变量值的变量属性?
var test = "test123"
var test123 ={
"key" + test: 123
}
Run Code Online (Sandbox Code Playgroud)
此代码不起作用."关键"+测试有什么问题?
创建Javascript对象的典型方法如下:
var map = new Object();
map[myKey1] = myObj1;
map[myKey2] = myObj2;
Run Code Online (Sandbox Code Playgroud)
我需要创建一个这样的映射,其中键和值都是字符串.我有一大堆但静态的对要添加到地图中.
有没有办法在Javascript中执行这样的操作:
var map = { { "aaa", "rrr" }, { "bbb", "ppp" } ... };
Run Code Online (Sandbox Code Playgroud)
或者我必须为每个条目执行类似的操作:
map["aaa"]="rrr";
map["bbb"]="ppp";
...
Run Code Online (Sandbox Code Playgroud)
基本上,剩余的Javascript代码将遍历此映射并根据"运行时"已知的标准提取值.如果这个循环作业有更好的数据结构,我也很感兴趣.我的目标是尽量减少代码.
我有一个对象
var Messages = {
'fullName' : 'Tell us your cool name dude..! e.g. Yousuf Iqbal',
'userName' : 'Choose a catchy username. Remember! It should be available :)',
'password' : 'Choose a top secret password with special chars, numbers and alphabets',
'rePassword' : 'Retype the password you just typed. But, don\'t try to copy!',
'bYear' : 'Tell the year, in which this bomb blasted'
};
Run Code Online (Sandbox Code Playgroud)
和一个变量..
var attribute = $('#userinfo form input').attr('name');
Run Code Online (Sandbox Code Playgroud)
现在我想使用这个变量选择Messages对象属性.
var message = Messages.attribute;
Run Code Online (Sandbox Code Playgroud)
但它没有工作..并且还尝试了以下..
var message …Run Code Online (Sandbox Code Playgroud) 我试图将数组元素设置为对象属性
简化示例:
var array = ['a', 'b', 'c'];
var obj = { array[1]: 'good' }
Run Code Online (Sandbox Code Playgroud)
上面会导致错误.
更新:事实上,我将对象作为另一个数组的一部分传递,即一个简化的例子是:
aObj[value] = ['one', {array[1]: 'good'}, 'two', 'three', 'four'];
Run Code Online (Sandbox Code Playgroud)
设置obj[array[1]] = 'good';样式意味着使用
aObj[value][1][array[1]] = 'good';
Run Code Online (Sandbox Code Playgroud) 我希望能够通过变量提供 JSON 变量名称及其值,而不是对其进行硬编码:
data: {
inputVar : inputVal
},
Run Code Online (Sandbox Code Playgroud)
但这样做并定义
inputVar = 'myInputVar';
Run Code Online (Sandbox Code Playgroud)
不起作用,而是直接输入 POST 变量名称才有效:
'myInputVar' : inputVal
Run Code Online (Sandbox Code Playgroud)
最后,我想要完成的是创建一个函数,通过在函数调用中提供 JSON 格式的 AJAX 变量和值,我可以调用它来进行 JSON 格式的 AJAX 调用:
makeAjaxJsonCall(fileName, postVar, postVal) {
$.ajax({
type : 'POST',
url : fileName,
dataType : 'json',
data: {
inputVar : inputVal
},
success : function(data) {
.
.
.
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
.
.
.
}
});
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
附注。我真的不太了解 JavaScript 编程,所以请在回复时准确提供完整的代码 - 谢谢!
我有一个对象,我想使用对象属性值作为键(简化):
var App = function() {
this.settings = {
'tetris': {
title: 'Tetris'
},
'galaxian': {
title: 'Galaxian'
}
};
this.gameName = 'tetris';
this.request = {
this.settings[this.gameName].title: 'THIS KEY IS INVALID' // :(
}
};
Run Code Online (Sandbox Code Playgroud)
我知道我可以将其分配为:
this.request[ this.settings[this.gameName].title ] = 'Valid...';
Run Code Online (Sandbox Code Playgroud)
但是我想知道是否可以使用对象属性值定义属性名称?
我正在使用 request.batch 运行 K6 测试,其中每个测试的请求数量可能会发生变化。
\n\nreq = [req0, req1, req2, ...];\nlet res = http.batch(req);\nRun Code Online (Sandbox Code Playgroud)\n\n然后,我尝试对每个请求运行“检查”,并使用 while 循环来执行此操作。
\n\nwhile (i < req.length) {\n check(\n res[i],\n {" ${i} - status 200": (r) => r.status === 200 }\n );\n i++;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n然而,K6 在单个测试中累积所有“检查”测试结果,因为测试消息不会解析我传递的变量。\n输出在测试结束时打印此消息:
\n\nreq = [req0, req1, req2, ...];\nlet res = http.batch(req);\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试过使用不同的方式添加参数,但没有用:
\n\n{ i + " - status 200": (r) => r.status === 200 }\n{\' ${i} - status 200\': (r) => r.status === 200 }\n{` ${i} …Run Code Online (Sandbox Code Playgroud) 我循环遍历jQuery选择器并尝试将表单元素的值分配给容器变量,然后可以将其作为JSON传递给进一步的操作.
假设有三组复选框,每组都在自己的div中,名为group1,group2和group3.
这是伪代码,但是像这样:
var data = {};
var groups = {
'group1': obj with a bunch of key val pairs for checkboxes,
'group2': another obj,
'group3': and another obj
}; // these define divs / groups of checkboxes
// create all the checkboxes and divs from the groups object somewhere in here
//now build a function to loop over the groups and get any checked boxes
function getInputs(){
$.each(groups, function(index, el) {
// this is where I am stuck
data.index …Run Code Online (Sandbox Code Playgroud)