-1

var c = {
'aa-bb': [{ a: 1, v: 2}],
'cc-xz': [{ c: 2}]
}
console.log(Object.keys(c))

I need to create an object, whose keys (the name) must be from Object.keys.

For each key name an object of type array must be defined as below.

Can you give me a hand?

result:

const res = {
  'aa-bb': Array(number).fill(0),
  'cc-xz': Array(number).fill(0)
};
1
  • 2
    where's your 'number' coming from? Commented Apr 22, 2021 at 12:12

2 Answers 2

0

var c = {
  'aa-bb': [{
    a: 1,
    v: 2
  }],
  'cc-xz': [{
    c: 2
  }]
}

const keys = Object.keys(c);
let res = {},
  number = 5;
keys.forEach(key => res[key] = Array(number).fill(0));
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

0

Map the keys, and create pairs of [key, array], and convert back to an object using Object.fromEntries():

const fn = (obj, arrLengh) =>
  Object.fromEntries(
    Object.keys(obj)
      .map(key => [key, Array(arrLengh).fill(0)])
  );

const c = {"aa-bb":[{"a":1,"v":2}],"cc-xz":[{"c":2}]};

const result = fn(c, 5);

console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.