0

I am trying to create an array of objects from the sample response received as const val with dynamic keys and values. I followed this which helped me with dynamic key columns; but I am unable to dynamically assign the values to the keys.

const key is dynamic and val.dims would always contain the exact same number of objects as the number of values in key. If key has 3 candidates; val would contain 3 objects.

I don't want to achieve this by hardcoding if possible.

const val = {
    "dims": [{
            "flOne": {},
            "values": [
                100,
                99,
                98,
                97,
                96              
            ]
        },
        {
            "flOne": {},
            "values": [
                1,
                2,
                3,
                4,
                5
            ]
        }
    ]
};


const keys = ['a', 'b'];
const lenOne = keys.length;
const base = val.dims[0].values;
const lenTwo = base.length;

console.log(lenOne, lenTwo);

const data = [];
let obj = {};

for (let i = 0; i < lenTwo; i++) {
    const vals = base
    keys.forEach(
        (a, j) => {
            obj[a] = vals[i];
            return obj;
        }
    )
    data.push(obj);
}

console.log(data);

I desire this [{ a: 1, b:100 }, { a: 2, b: 99 }, { a: 3, b: 98 }, { a: 4, b: 97 }, { a: 5, b: 96 }]

smpa01
  • 4,149
  • 2
  • 12
  • 23

2 Answers2

0

There are many ways to achieve this, using reduce is what comes to mind:

const val = {
  "dims": [{
      "flOne": {},
      "values": [
        100,
        99,
        98,
        97,
        96
      ]
    },
    {
      "flOne": {},
      "values": [
        1,
        2,
        3,
        4,
        5
      ]
    }
  ]
};


const keys = ['a', 'b'];
const base = val.dims[0].values;

const data = base.map((_, idx) => (
  keys.reduce((acc, curr, i) => ({
    ...acc,
    [curr]: val.dims[i].values[idx]
  }), {})
))

console.log(data)
Damzaky
  • 6,073
  • 2
  • 11
  • 16
  • small thing is that a and b have to be switched but that can also be achieved by switching the keys – cmgchess Apr 05 '23 at 04:36
  • @cmgchess well OP said that number of keys can be dynamic "If key has 3 candidates...", OP didn't say in what order it would be if it's more than 2, so I just assumed the expected output OP wrote was not in the correct order. But let's see what OP would respond to this – Damzaky Apr 06 '23 at 04:03
0

You can use map and for loop to achieve this

const val = {
    "dims": [{
            "flOne": {},
            "values": [
                100,
                99,
                98,
                97,
                96              
            ]
        },
        {
            "flOne": {},
            "values": [
                1,
                2,
                3,
                4,
                5
            ]
        }
    ]
};


const keys = ['a', 'b'];
const result = [];
for(let i=0; i < keys.length; i ++){
   val.dims[i].values.map((item, index) => {
    if(result[index]){
        result[index][keys[i]] = item
    }
    else {
        result[index] = {
            [keys[i]]: item
        }
    }
})
}

console.log(result);
Srushti Shah
  • 810
  • 3
  • 17