简体   繁体   English

更改jQuery对象的“名称”属性

[英]Changing 'name' property of jquery object

I'm having an issue with a Javascript object. 我在使用Javascript对象时遇到问题。 When I use push on the object, both parts should take on the value of a variable. 当我在对象上使用push时,两个部分都应取一个变量的值。 For some reason though, only valueOfName seems to change based on the variable. 但是由于某种原因,似乎只有valueOfName会根据变量进行更改。 I need name to change as well, at the moment it seems to take whatever value it is written as with or without quotes. 我还需要更改name ,此刻,它似乎采用带引号或不带引号的任何值。 Here is the relevent code. 这是相关的代码。

var obj = {};
obj.videoID = [];
var terms = ['best','awesome','incredible','insane','great'];
$.each(terms, function(cur, val){
    $.each(data.items, function(i, object) {
        $.each(object.id, function(property, value) {
            obj.videoID.push({val:value});
        });
    });
});

Am I going about this wrong? 我要解决这个错误吗? Or is there something I've missed? 还是我想念的东西? Thanks in advance for your help guys. 在此先感谢您的帮助。

Create a object and use Bracket notation to create property with dynamic name, afterwards push() the object to array. 创建一个对象,并使用Bracket表示法创建具有动态名称的属性,然后push()对象push()数组。

$.each(object.id, function(property, value) {
    var dummy = {}; //Create object
    dummy[val] = value; //create property and set value
    obj.videoID.push(dummy);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM