在JavaScript中将数据构建为多维数组

时间:2021-10-19 01:37:17

I'm trying to rebuild data structure from an existing JSON file. For the sake of example, I trimmed all the unnecessary code:

我正在尝试从现有的JSON文件重建数据结构。为了举例,我修剪了所有不必要的代码:

var entries = [
  {
    "band": "Weezer",
    "song": "El Scorcho"
  },
  {
    "band": "Chevelle",
    "song": "Family System"
  }
]

var rows = {};

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

    var entry = entries[i];

    var t = "a";
    for (var key in entry) {
            rows[t] = t;
            t = t+"1";
    }    
    $("#agenda").append(JSON.stringify(rows));

}
$("#agenda").append("<br /><br /> In the end, it only shows the last one:<br />");
$("#agenda").append(JSON.stringify(rows));

There's also a fiddle that shows it better: http://jsfiddle.net/84w6F The aim of this example is to try and rebuild the data in "entries" to be exactly the same, by calling both the key and the value as variables. For some reason, I end up with a mess , and in the end when I try to read the array after the loop, it shows me only the last sub array.

还有一个小提琴可以更好地显示它:http://jsfiddle.net/84w6F这个例子的目的是通过调用键和值作为变量来尝试重建“条目”中的数据完全相同。出于某种原因,我最终弄得一团糟,最后当我尝试在循环之后读取数组时,它只显示最后一个子数组。

2 个解决方案

#1


1  

You have a 1 dimensional array containing 2 entries here, not a multidimensional array.

你有一个包含2个条目的1维数组,而不是一个多维数组。

Your outer loop, is iterating over the two objects in the array fine, and the inner loop is going over all the key value pairs in each object but it is only setting rows["a"] and rows["a1"] because each object in the array only has 2 properties.

你的外部循环正在迭代数组中的两个对象,并且内部循环遍历每个对象中的所有键值对,但它只设置行[“a”]和行[“a1”],因为每个数组中的对象只有2个属性。

I'm not entirely sure what you want to do with the data within the array, if you want to copy them completely then you can do something like this:

我不完全确定你想要对数组中的数据做什么,如果你想完全复制它们,那么你可以这样做:

var rows = [];

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

    var entry = entries[i];

    var newObj = {};
    for (var key in entry) {
      newObj[key] = entry;
    }
    rows.push(newObj);
}

#2


0  

It is working as intended. Since there are 2 entries, you are appending the rows twice inside the loop:

它按预期工作。由于有2个条目,因此您将在循环中将行追加两次:

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

    var entry = entries[i];

    var t = "a";
    for (var key in entry) {
            rows[t] = t;
            t = t+"1";
    }    
    $("#agenda").append(JSON.stringify(rows));

}

What you are actually doing is that you are replacing rows['a'] and rows['a1'] repeatedly (instead of growing it) and appended it twice so that you saw the first result.

你实际在做的是你要反复替换行['a']和行['a1'](而不是增长它)并将它追加两次以便你看到第一个结果。

#1


1  

You have a 1 dimensional array containing 2 entries here, not a multidimensional array.

你有一个包含2个条目的1维数组,而不是一个多维数组。

Your outer loop, is iterating over the two objects in the array fine, and the inner loop is going over all the key value pairs in each object but it is only setting rows["a"] and rows["a1"] because each object in the array only has 2 properties.

你的外部循环正在迭代数组中的两个对象,并且内部循环遍历每个对象中的所有键值对,但它只设置行[“a”]和行[“a1”],因为每个数组中的对象只有2个属性。

I'm not entirely sure what you want to do with the data within the array, if you want to copy them completely then you can do something like this:

我不完全确定你想要对数组中的数据做什么,如果你想完全复制它们,那么你可以这样做:

var rows = [];

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

    var entry = entries[i];

    var newObj = {};
    for (var key in entry) {
      newObj[key] = entry;
    }
    rows.push(newObj);
}

#2


0  

It is working as intended. Since there are 2 entries, you are appending the rows twice inside the loop:

它按预期工作。由于有2个条目,因此您将在循环中将行追加两次:

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

    var entry = entries[i];

    var t = "a";
    for (var key in entry) {
            rows[t] = t;
            t = t+"1";
    }    
    $("#agenda").append(JSON.stringify(rows));

}

What you are actually doing is that you are replacing rows['a'] and rows['a1'] repeatedly (instead of growing it) and appended it twice so that you saw the first result.

你实际在做的是你要反复替换行['a']和行['a1'](而不是增长它)并将它追加两次以便你看到第一个结果。