Frank
Frank

Reputation: 442

javascript. Iterate in array with map with variable items

Sorry if duplicated some issue, but I searched a lot and didn't find anything. I have to fill a table in js. To do this, I take the values of an array of objects (centers) and apply map. Everything works perfectly.

centers = [
        { 'center': 'Center 1',
          'datos': { "key1":4.67,
                     "key2":3.56
                    } 
        },

        { 'center': 'Center 2',
          'datos': { "key1":0.34,
                     "key2":5} 
                    },

        { 'center': 'Center 3',
          'datos': { "key1":3.7,
                     "key2":2.5} 
                    }
        ]

let result = centers.map(res => {
    let columns = {'column1':res.datos.key1, 'column2':res.datos.key2}
    return columns;
})

/* result =  [
  [
    { column1: 4.67, column2: 3.56 },
    { column1: 0.34, column2: 5 },
    { column1: 3.7, column2: 2.5 }
  ]
]

*/

However, the items "key1 ...... key1000" are many and I would like to implement them in the map function like this

var cc = ['key1','key2','key3','key4','key5'];

let result = centers.map(res => {
    let columns = {'column1':res.datos.cc[0], 'column2':res.datos.cc[1], ...........}
    return columns;
})

but it does not take the value of the variable. It's possible with map this?. Thank you.

Upvotes: 1

Views: 697

Answers (3)

bel3atar
bel3atar

Reputation: 943

const centers = [{
  'center': 'Center 1',
  'datos': {
    "key1": 4.67,
    "key2": 3.56
  }
}, {
  'center': 'Center 2',
  'datos': {
    "key1": 0.34,
    "key2": 5
  }
}, {
  'center': 'Center 3',
  'datos': {
    "key1": 3.7,
    "key2": 2.5
  }
}]

const result = centers.map(res => Object.entries(res.datos).reduce((acc, [key, value]) => ({ ...acc,
  [key.replace('key', 'column')]: value
}), {}))
console.log(result)

Upvotes: 1

Frank
Frank

Reputation: 442

I have finally been able to solve. I post in case it can be of help

centers = [
        { 'center': 'Center 1',
          'datos': { "key1":4.67,
                     "key2":3.56
                    } 
        },

        { 'center': 'Center 2',
          'datos': { "key1":0.34,
                     "key2":5} 
                    },

        { 'center': 'Center 3',
          'datos': { "key1":3.7,
                     "key2":2.5} 
                    }
        ]


 var cc = ['key1','key2'];

for (let i = 0; i < cc.length; i++) {
        
centers.map(res => {
    let nombreColumna = "column"+i;
    let columns = {[nombreColumna]:res.datos[cc[i]]}
     arrayOut.push (columns);
    return arrayOut;
})
}

/*
arrayOut:

[
  [
    { column0: 4.67 },
    { column0: 0.34 },
    { column0: 3.7 },
    { column1: 3.56 },
    { column1: 5 },
    { column1: 2.5 }
  ]
]

´´´

Upvotes: 0

HijenHEK
HijenHEK

Reputation: 1296

you need to use Object.keys(res) to get all keys in an array here cc then use dynamic variable names in a for loop first to name the columns (starts from (column1) and to name the keys starts from 0 in cc and 1 in names cc[0] => key1


let result = centers.map(res => {

    cc = Object.keys(res); // ['key1','key2','key3','key4','key5']
    let columns : {};

    for (let i = 0; i < cc.length; i++) {
       // for i = 0 =>  columns['column1'] = res.datos[key0]
       // for i = 0 =>  columns.columns1 = res.datos.key0
        columns['column' + (i + 1)] = res.datos[`${cc[i]}`] ;
    
    }

    return columns ;

}

Upvotes: 0

Related Questions