以编程方式从数据对象创建变量

时间:2019-08-09 22:51:17

标签: javascript

我要从Google电子表格中提取一堆数据,需要一种方法以编程方式为每个单元格创建一个变量,其中名称是单元格(即A2),值是单元格的内容。

应该有可能,因为我需要的一切都在那里。例如,data[11].title.$t是单元格名称(在这种情况下为A2),而data[11].content.$t是单元格的内容。

我可以手动完成所有操作:var A2 = data[11].content.$t等,但是我觉得有更好的方法。

$(document).ready(function() {
    var url = "https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic?alt=json";
    $.ajax({
        url:url,
        dataType:"jsonp",
        success:function(data) {
            var data = data.feed.entry;
            console.log(data);
        }
    });
});

1 个答案:

答案 0 :(得分:1)

正如一些评论者所言,最好创建一个可以引用或传递的对象。这种数据结构是否最适合表示二维表,将取决于您要如何处理。

您可以使用for循环或Array.forEach遍历整个数组,提取名称和值,然后填充对象:

const entries = data.feed.entry
const result = {}
entries.forEach(entry => {
  const value = entry.content.$t,
        name  = entry.title.$t
  result[name] = value
})

console.log(result)

const sample = [{"id":{"$t":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C1"},"updated":{"$t":"2019-08-09T22:58:57.024Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#cell"}],"title":{"type":"text","$t":"A1"},"content":{"type":"text","$t":"Gauge Name"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C1"}]},{"id":{"$t":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C2"},"updated":{"$t":"2019-08-09T22:58:57.024Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#cell"}],"title":{"type":"text","$t":"B1"},"content":{"type":"text","$t":"Previous value"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C2"}]},{"id":{"$t":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C3"},"updated":{"$t":"2019-08-09T22:58:57.024Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#cell"}],"title":{"type":"text","$t":"C1"},"content":{"type":"text","$t":"Current Value"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C3"}]},{"id":{"$t":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C4"},"updated":{"$t":"2019-08-09T22:58:57.024Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#cell"}],"title":{"type":"text","$t":"D1"},"content":{"type":"text","$t":"Low"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C4"}]},{"id":{"$t":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C5"},"updated":{"$t":"2019-08-09T22:58:57.024Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#cell"}],"title":{"type":"text","$t":"E1"},"content":{"type":"text","$t":"Underperform"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/cells/1dKiMlM1KXnY6zjh75CUoe3KTdKuGBlaFZXfFWYxtCmM/od6/public/basic/R1C5"}]}]

const result = {}
sample.forEach(entry => {
  const value = entry.content.$t,
    name  = entry.title.$t
  result[name] = value
})
// two ways of getting the value
console.log(result)

尽管Javascript很疯狂,但这样做的后果自负,但您可以通过window[name](相对于result[name])在浏览器中创建全局变量,但是有些编译器可能会抱怨和/或使用严格的模式可能会使代码抛出引用错误,但这通常不是一个好主意。