如何使用循环创建动态对象键/值?

时间:2019-09-24 10:42:27

标签: javascript arrays object

我是javascript的新手,因此会有所作为。我必须使用“ react-data-grid”库创建表数据,然后才需要列数组和行对象。

问题是我的表有column * rows = 80 * 25表 我的行包含这样的数据

let rows = [];
rows.push( { 0: "A", 1: "S", 2: "D" , 3: "F", ......., 80 : "P" );
rows.push( { 0: "Z", 1: "X", 2: "C" , 3: "V", ......., 80 : "L" );
.
.
.
rows.push( { 0: "Q", 1: "W", 2: "E" , 3: "R", ......., 80 : "M" );

我试图像下面的代码那样循环。

const rows = [];
    for (let row = 0; row <= 25; row++){
        let objects = {};
        for (let x = 0; x < 80; x++) {
            objects[x] = {x: " "};
        }
        rows.push(objects)
    }

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情吗?

const rows = [];
    for (let row = 0; row < 25; row++){
        let objects = {};
        for (let x = 0; x < 80; x++) {
            objects[x] = " ";
        }
        rows.push(objects)
    }

区别在于,您在上面的示例中创建的是对象而不是键/值。

答案 1 :(得分:0)

您可以使用

@FetchRequest(entity: Post.entity(), sortDescriptors: [ 
    NSSortDescriptor(keyPath: \Post.date, ascending: true)
]) 
var posts: FetchedResults<Post> // Use of undeclared type ´Post´

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map