阵列对象和背面的数组

时间:2015-01-02 22:33:45

标签: javascript json

我需要一个通用方法来获取(数组数组)+(头数组)并将其转换为JSON对象数组,然后将其转换回数组数组。 '解包' /'包' http://www.thomasfrank.se/SQLike/的SQLike库中的方法执行此操作。出于各种原因,我真的不喜欢这个图书馆而更愿意使用别的东西。有谁能推荐一个解决方案?

澄清一下,我想在这两个数据'之间来回走动:

1) var headers = ['col_1', 'col_2'] 
   var data = [['x0', 0.34],['x1', 0.35]]

2) var objArr = [[{col_1: 'x0'},{col_2: 0.34}], [{col_1: 'x1', col_2: 0.35}]]

我无法每次都创建对象模板,因为我正在从Google工作表中读取各种数据。因此,我需要一种通用的方法来做到这一点。

4 个答案:

答案 0 :(得分:3)

您可以使用named数组从数组中的数据创建对象中的属性:

function toObjects(headers, data) {
  var result = [];
  for (var i = 0; i < data.length; i++) {
    var o = {};
    for (var j = 0; j < headers.length; j++) {
      o[headers[j]] = data[i][j];
    }
    result.push(o);
  }
  return result;
}

然后使用标题从对象中读取属性并放入数组:

function toArray(headers, data) {
  var result = [];
  for (var i = 0; i < data.length; i++) {
    var a = [];
    for (var j = 0; j < headers.length; j++) {
      a.push(data[i][headers[j]]);
    }
    result.push(a);
  }
  return result;
}

答案 1 :(得分:1)

http://jsfiddle.net/ryanwheale/tp3nk4zf/2/

var headers = ['col_1', 'col_2'];
var data = [['x0', 0.34], ['x1', 0.35]];

function unpack(headers, data) {
    var unpacked = [], obj, i, il, n, nl;
    for (i = 0, il = data.length; i < il; i++) {
        obj = {};
        for (n = 0, nl = headers.length; n < nl; n++) {
            obj[headers[n]] = data[i][n];
        }

        unpacked.push([obj]);
    }

    return unpacked;
}

function pack(headers, data) {
    var packed = [], arr, i, il, n, nl;
    for (i = 0, il = data.length; i < il; i++) {
        arr = [];
        for (n = 0, nl = headers.length; n < nl; n++) {
            arr.push(data[i][0][ headers[n] ]);
        }
        packed.push(arr);
    }
    return packed;
}

var unpacked = unpack(headers, data);
var packed = pack(headers, unpacked);
console.log(unpacked, packed);

答案 2 :(得分:0)

这应该有效。请注意,正如Guffa在注释中提到的那样,pack中数组内部的值的顺序,即生成头数组的顺序,并不保证与解包时的顺序相同,但标题和值确实匹配。

function unpack(headers, values) {
    var result = [];
    for(var v = 0; v < values.length; v++) {
        var arr = values[v];
        var obj = {};
        for(var i = 0; i < headers.length; i++) {
            obj[headers[i]] = arr[i];
        }
        result.push(obj);
    }
    
    return result;
}

function pack(values) {
    var result = [];
    var headers = null;
    for(var v = 0; v < values.length; v++) {
        var obj = values[v];
        if(headers == null) {
            headers = [];
            for(var p in obj) {
                headers.push(p);
            }
        }

        var arr = [];
        for(var i = 0; i < headers.length; i++) {
            arr[i] = obj[headers[i]];
        }

        result.push(arr);
    }
    
    return { headers: headers, data: result };
}


var headers = ['col_1', 'col_2'] ;
var data = [['x0', 0.34],['x1', 0.35]];

var unpacked = unpack(headers, data);
console.log(JSON.stringify(unpacked));

var objArr = [{col_1: 'x0', col_2: 0.34}, {col_1: 'x1', col_2: 0.35}];
var packed = pack(objArr);
console.log(JSON.stringify(packed));

答案 3 :(得分:0)

这应该可以正常工作=)

&#13;
&#13;
var pack = function(headers, data) {
  return data.map(function(row) {
    return row.map(function(column, i) {
      return Object.defineProperty({}, headers[i], {value: column});
    });
  });
};
var unpack = function (packed) {
  var headers = [];
  return {
    data: packed.map(function(row) {
      return row.map(function(column, i) {
        var key = Object.keys(column)[0];
        if (headers.length !== row.length) {
          headers.push(key);
        }
        return column[key];
      });
    }),
    headers: headers,
  };
};
console.log('pack', pack(['col_1', 'col_2'], [['x0', 0.34], ['x1', 0.35]]));
console.log('unpack', unpack([[{col_1: 'x0'}, {col_2: 0.34}], [{col_1: 'x1'}, {col_2: 0.35}]]));
&#13;
&#13;
&#13;