简体   繁体   English

如何动态地向javascript数组中的每个对象添加属性

[英]How to add properties dynamically to each object in an javascript array

I am trying to loop through an array of objects to prepend a property and value to each object . 我试图遍历对象数组,为每个对象添加属性和值。 The order of the tables is important because I am trying to use a handsontable view as a client to retrieve the contents of a server side mysql table. 表的顺序很重要,因为我试图将handontable视图用作客户端来检索服务器端mysql表的内容。 I want the handsontable view to have the same column order as the table , but I want to insert a checkbox column as the first column to allow record selection. 我希望handsontable视图具有与表相同的列顺序,但是我想插入复选框列作为第一列以允许选择记录。 I have a fiddle http://jsfiddle.net/kc11/juo1e4at/#base that does the loop, but I'm not sure how to prepend each object's property-value pair. 我有一个小提琴http://jsfiddle.net/kc11/juo1e4at/#base来执行循环,但是我不确定如何在每个对象的属性-值对之间添加前缀。 array unshift appears to be for arrays. 数组取消移位似乎是针对数组的。 I'd like a result of an object to look go from: 我想要一个对象的结果来自:

Object { car="Audi A4 Avant", year=2011, available=true, more...}

to: 至:

Object { checkbox=false, car="Audi A4 Avant", year=2011, available=true, more...}

How can I make this happen 我怎样才能做到这一点

The order of properties in a Javascript object doesn't matter, they aren't strictly ordered. Javascript对象中属性的顺序无关紧要,它们不是严格排序的。
Given 特定

var cars = [
  {car: "Mercedes A 160", year: 2006, available: true, comesInBlack: 'yes'},
  {car: "Citroen C4 Coupe", year: 2008, available: false, comesInBlack: 'yes'},
  {car: "Audi A4 Avant", year: 2011, available: true, comesInBlack: 'no'},
  {car: "Opel Astra", year: 2004, available: false, comesInBlack: 'yes'},
  {car: "BMW 320i Coupe", year: 2011, available: false, comesInBlack: 'no'}
];

You can simply do the following if you don't have the property ready to send from the backend or want to do it in the frontend for other reasons (smaller payload size etc). 如果您没有准备好从后端发送的属性,或者由于其他原因(较小的有效负载大小等)而希望在前端进行发送,则可以简单地执行以下操作。

for (i in cars) {
  cars[i].checkbox = false;
}

Now, coming back to the issue of Handsontable column order (which is actually the main problem of the question), you can define it as follows: 现在,回到Handsontable列顺序的问题(实际上是问题的主要问题),您可以按以下方式定义它:

var hot = new Handsontable(container,{
  data: cars,
  /* Other config */
  columns: [
    {data: "checkbox", type: 'checkbox'},
    {data: "car", type: 'text'}
    /*Other columns*/
  ]
});

See the manual for further info. 有关更多信息,请参见手册

Here is an example using the getCarData function in your fiddle. 这是在小提琴中使用getCarData函数的示例。 It allows you to prepend keys to an object by wrapping this functionality in a simple class (called PrependableObject ). 它允许您通过将功能包装在一个简单的类(称为PrependableObject )中来在键之前添加键。

PrependableObject = function(obj){

    this.obj = obj;

}

PrependableObject.prototype.prepend = function(key, val){

    var newObj  = new Object(),
        oldKeys = Object.keys(this.obj);

    newObj[key] = val;

    for (var i=0; i< oldKeys.length; i++){

        newObj[oldKeys[i]] = this.obj[oldKeys[i]];
    }

    return newObj;

}

function getCarData() {
    return [
      {car: "Mercedes A 160", year: 2006, available: true, comesInBlack: 'yes'},
      {car: "Citroen C4 Coupe", year: 2008, available: false, comesInBlack: 'yes'},
      {car: "Audi A4 Avant", year: 2011, available: true, comesInBlack: 'no'},
      {car: "Opel Astra", year: 2004, available: false, comesInBlack: 'yes'},
      {car: "BMW 320i Coupe", year: 2011, available: false, comesInBlack: 'no'}
    ];
 }

var carData = getCarData();

carData.forEach(function(obj, index){

    var obj = new PrependableObject(obj),
        newObj = obj.prepend('check', false);

    carData[index] = newObj;
});

// To prove the keys are in the correct order
var car1 = carData[0];

Object.keys(car1).forEach(function(key, i){

    console.log(key + ' = ' + car1[key]);

});

Updated fiddle: 更新的小提琴:

http://jsfiddle.net/juo1e4at/4/ http://jsfiddle.net/juo1e4at/4/

Of course, the logic in your forEach loop could do anything (eg use the car model to determine which dynamic value to assign the object, the car year, etc). 当然, forEach循环中的逻辑可以做任何事情(例如,使用汽车模型来确定要分配对象的动态值,汽车年份等)。

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

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