简体   繁体   中英

How to create a dynamic object key/value with a loop?

I'm a new for javascript and react. I have to use "react-data-grid" library to create table sheet data then it's required Array of column and object of row.

The problem is my table have column*rows = 80*25 table my row include of data like this

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" );

I try to loop it's like code below.

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

Can you try something like this?

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

The difference is that you are creating an object instead of a key / value in your example above.

You can use

const mapObject = new Map();
mapObject.set('any key', 'any value');
mapObject.set('any key 2', 'any value 2');
mapObject.get('any key'); // any value

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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