简体   繁体   English

遍历javascript对象并动态添加对象属性(JS对象中的键值对)

[英]Iterate through javascript object and add object properties dynamically (key-value pair in JS object)

I am not sure if this question has been answered earlier. 我不确定这个问题是否早已得到回答。 Although i didn't find any relevance to what i wanted. 虽然我没有找到与我想要的东西相关的东西。 here's the original code. 这是原始代码。

function addrow() {
                  var row = {
                             name: null,
                             city: null,
                             Contact: null,
                             location: null             
                  };

                  $("#jqxgrid").jqxGrid('addrow', null, row, 'first');
}

This code adds a new row to the grid table once this function executes by click function. 一旦此函数通过单击功能执行,此代码会将新行添加到网格表。 It creates a empty row. 它创建一个空行。 The column names are name, city, Contact and location. 列名称是名称,城市,联系人和位置。 All these columns entries row gets null value and empty row gets added to the grid. 所有这些列条目的行都将获得空值,并且将空行添加到网格中。

I want to make this dynamic. 我想让它充满活力。 ie irrespective of the column names and number of columns, it should create empty row for me. 即,无论列名和列数如何,它都应该为我创建一个空行。 for eg. 例如 if there are only 2 column names named Name and Age, it should create a row of 2 empty cells. 如果只有2个名为Name和Age的列名,则应创建2个空单元格的行。

I tried something like this to make it dynamic. 我尝试了类似的方法使其具有动态性。

var headers= $line_array. 

i get the dynamic column header names from an array passed by back-end to front-end. 我从后端传递到前端的数组中获取动态列标题名称。 So the column headers are present in an array named headers. 因此,列标题存在于名为标题的数组中。 Then i added this 然后我添加了这个

for(key in row) { headers[key]=null,};
$("#jqxgrid").jqxGrid('addrow', null, row, 'first'); 

So the final addrow() function looks like this 所以最终的addrow()函数如下所示

function addrow() {

var headers= $line_array;

var i;
var key = headers.length;
var row = {}; // create empty object. we can add header names inside this row for display.

for(key in row) { 
    headers[key]=null,};
    $("#jqxgrid").jqxGrid('addrow', null, row, 'first');
}

Can someone please suggest me what minor mistake i am committing over here ? 有人可以建议我在这里犯什么小错误吗?

I am glad i found the answer myself. 我很高兴自己找到了答案。 Although inputs from @MaxZoon and @RicharGrant helped me understand what might be the issue. 尽管@MaxZoon和@RicharGrant的输入帮助我了解了可能是什么问题。

If this code helps anyone, i will be happy. 如果此代码可以帮助任何人,我将很高兴。 here it is. 这里是。

function addrow() {
    var headers= $line_array;
    var i;
    var key = headers.length;
    var row = {};

    for (i=0; i <headers.length; i++) {
                                       row[headers[i]]=null;
    }

    $("#jqxgrid").jqxGrid('addrow', null, row, 'first');
}

This is it. 就是这个。 It was as simple as that. 就这么简单。 I just had to take a look at how a key/value pair is assigned to a Javascript object. 我只需要看一下如何将键/值对分配给Javascript对象。 Thanks all. 谢谢大家

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

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