我正在尝试使用动态名称访问对象的属性.这可能吗?
const something = { bar: "Foobar!" };
const foo = 'bar';
something.foo; // The idea is to access something.bar, getting "Foobar!"
Run Code Online (Sandbox Code Playgroud) 我使用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使用变量命名对象的属性?
JavaScript让我感到惊讶,这是另一个例子.我刚刚遇到了一些我最初没有理解的代码.所以我调试了它并得出了这个发现:
alert('a'['toUpperCase']()); //alerts 'A'
Run Code Online (Sandbox Code Playgroud)
现在这必须是明显的,如果toUpperCase()被定义为字符串类型的成员,但它最初对我没有意义.
无论如何,
toUpperCase是'a'的成员?或者幕后还有其他事情发生?我正在阅读的代码具有如下功能:
function callMethod(method) {
return function (obj) {
return obj[method](); //**how can I be sure method will always be a member of obj**
}
}
var caps2 = map(['a', 'b', 'c'], callMethod('toUpperCase')); // ['A','B','C']
// ignoring details of map() function which essentially calls methods on every
// element of the array and forms another array of result and returns it
Run Code Online (Sandbox Code Playgroud)
它是在ANY对象上调用ANY方法的通用函数.但这是否意味着指定的方法已经是指定对象的隐式成员?
我确信我对JavaScript函数的基本概念缺乏认真的理解.请帮我理解这个.
这是一系列关于JavaScript中语法的问题.这也是社区Wiki,因此邀请每个人参与维护此列表.
Stack Overflow不允许搜索特定字符.因此,在搜索运算符和其他语法标记时,很难找到许多关于运算符和其他语法标记的问题.这也使得关闭重复更加困难.以下列表是为了解决此问题.
主要思想是在Stack Overflow上链接现有问题,因此我们更容易引用它们,而不是复制ECMAScript规范中的内容.
此外,这是PHP符号引用的公然副本.我们需要一个JS.
请帮忙.编辑并添加指向其他运算符/语法参考的链接,或者如果您无法在特定语法上找到好的问题/答案,请添加此问题的答案并将其链接
我有以下json对象:
{ "id": "109",
"No. of interfaces": "4" }
Run Code Online (Sandbox Code Playgroud)
以下行正常工作:
alert(obj.id);
alert(obj["id"]);
Run Code Online (Sandbox Code Playgroud)
但如果键有空格,那么我无法访问它们的值,例如
alert(obj."No. of interfaces"); //Syntax error
Run Code Online (Sandbox Code Playgroud)
如何访问其键名具有空格的值?它甚至可能吗?
以下行显然用点表示法写得最好.我正在尝试清理我的JavaScript代码以使其严格.这是什么意思?
if (ie||ns6)
{
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : "";
}
Run Code Online (Sandbox Code Playgroud)
我在我的代码行中添加了一些上下文,以防这有用吗?我对DOM一无所知.我不是要支持Internet Explorer 4,这不是我的代码,我自己也无法编写JavaScript.我只是试图让它符合规范,JSLint工具说明了这一行:
第17行字符43的问题:['dhtmltooltip']最好用点表示法编写.
我有一个我无法丢弃的集合,我假设其名称中的" - "是一个特殊字符.在MongoDB中,逃避特殊字符的最佳方法是什么?
> db.tweets.drop();
true
Run Code Online (Sandbox Code Playgroud)
但
> db.tweets-old.drop();
ReferenceError: old is not defined (shell):1
Run Code Online (Sandbox Code Playgroud)
我试图用引号(单引号和双引号)和斜线来逃避,但没有任何作用.
我可以做这个:
var objPosition = {};
objPosition.title = "whatever";
Run Code Online (Sandbox Code Playgroud)
但是我动态地获得了"标题",并且想要使用大约六个这样获得的字符串来将六个属性分配给对象.我已经尝试了eval和其他一些似乎有相同问题的方案,但到目前为止还是空了.
我有:
var txtCol = $(this).text();
txtCol = $.trim(txtCol);
Run Code Online (Sandbox Code Playgroud)
我希望txtCol的值是属性名称.
有任何想法吗?
使用[]和.访问数组或对象属性的真正区别是什么?哪一个使用?
另外为什么.运营商不允许索引属性?
我目前正在开发一个使用API来检索,更新和删除数据的项目.我正在使用的API是prestashop API.因此,在能够检索数据并更新某些项目之后,我偶然发现了一个问题.正如文档中所述,通过API发送和检索的所有数据都是json和.xml由于API的某些数据在json返回中具有不同的级别,如@attributes和@associations级别,我提出了这个问题.
问题是我想访问这些数据,并结合angularjs我想显示这些数据.所以,让我向您展示一个我正在努力实现的快速示例.
首先,JSON回报将是这样的.
{"products":{"product":[{"id":"1","id_manufacturer":"1","id_supplier":"1","id_category_default":"5","new":{},"cache_default_attribute":"1","id_default_image":"1","id_default_combination":"1","id_tax_rules_group":"1","position_in_category":"0","manufacturer_name":"Fashion Manufacturer","quantity":"0","type":"simple","id_shop_default":"1","reference":"demo_1","supplier_reference":{},"location":{},"width":"0.000000","height":"0.000000","depth":"0.000000","weight":"0.000000","quantity_discount":"0","ean13":"333456789111","isbn":{},"upc":{},"cache_is_pack":"0","cache_has_attachments":"0","is_virtual":"0","state":"1","on_sale":"0","online_only":"0","ecotax":"0.000000","minimal_quantity":"1","price":"16.510000","wholesale_price":"4.950000","unity":{},"unit_price_ratio":"0.000000","additional_shipping_cost":"0.00","customizable":"0","text_fields":"0","uploadable_files":"0","active":"1","redirect_type":"404","id_type_redirected":"0","available_for_order":"1","available_date":"0000-00-00","show_condition":"0","condition":"new","show_price":"1","indexed":"1","visibility":"both","advanced_stock_management":"0","date_add":"2017-03-16 14:36:24","date_upd":"2017-12-01 13:01:13","pack_stock_type":"3","meta_description":{"language":{"@attributes":{"id":"1"}}},"meta_keywords":{"language":{"@attributes":{"id":"1"}}},"meta_title":{"language":{"@attributes":{"id":"1"}}},"link_rewrite":{"language":"gebleekte-T-shirts-met-korte-mouwen"},"name":{"language":"Gebleekte T-shirts met Korte Mouwen"},"description":{"language":"
Fashion maakt goed ontworpen collecties sinds 2010. Het merk biedt vrouwelijke combineerbare kleding en statement dresses en heeft een pr\u00eat-\u00e0-porter collectie ontwikkeld met kledingstukken die niet in een garderobe mogen ontbreken. Het resultaat? Cool, gemakkelijk, easy, chique met jeugdige elegantie en een duidelijk herkenbare stijl. Alle prachtige kledingstukken worden met de grootste zorg gemaakt in Itali\u00eb. …Run Code Online (Sandbox Code Playgroud) javascript ×9
syntax ×3
json ×2
object ×2
angularjs ×1
function ×1
jquery ×1
mongodb ×1
prestashop ×1
properties ×1
web-services ×1