s. srichomthong
s. srichomthong

Reputation: 495

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)
    }

Upvotes: 0

Views: 918

Answers (2)

Sonjoy Samadder
Sonjoy Samadder

Reputation: 77

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

Upvotes: 0

Matheus Mello
Matheus Mello

Reputation: 320

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.

Upvotes: 1

Related Questions