0

am new to JavaScript and really struggling to nest some values of key-value pairs in a given object, like Basis (see below) by taking a key (for example "a") and adding a nested key (for example "e") as below and allocate the related value from basis to the added nested key. For each, a new object should be added in the same hierarchy to wrap it in an array.

Example

// Basis could look like this
{ a: '1', b: '2', c: '3', d: '4' }

// Map through basis and add new nested keys 
{ a: 'e', b: 'f', c: 'g', d: 'h', e: 'i', f: 'j', g: 'k' }

// Objective    
{ a: [{e: '1', date: Date.now()}], b: [{f: '2', date: Date.now()}], c: [{g: '3', date: Date.now()}], d: [{h: '4', date: Date.now()}] }

It is hard. I have no idea. Even the following source did not help me as I have to loop through a given object with key-value pairs. Javascript: how to dynamically create nested objects using object names given by an array

Paul M.
  • 183
  • 7

4 Answers4

3

Iterate through the elements of basis. Use the keys to find the corresponding element in nested, and use that as the key in the new object being created.

const basis = { a: '1', b: '2', c: '3', d: '4' };
const nested = { a: 'e', b: 'f', c: 'g', d: 'h', e: 'i', f: 'j', g: 'k' };

const result = Object.entries(basis).reduce((obj, [key, value]) => {
  obj[key] = [{
    [nested[key]]: value,
    date: Date.now()
  }];
  return obj;
}, {});

console.log(result);

[nested[key]]: means to use the value of nested[key] as the property name in the new object.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Something like this:

// Basis could look like this
const basis = { a: '1', b: '2', c: '3', d: '4' }

// Map through basis and add new nested keys 
const map = { a: 'e', b: 'f', c: 'g', d: 'h', e: 'i', f: 'j', g: 'k' }

const objective = {};
for (let [key, value] of Object.entries(map)) {
  if(basis[key]) {
    let item = { 
      date: Date.now()
    };
    item[value] = basis[key];
    objective[value] = [item];
  }
}
console.log(objective);
// Objective    
//{ a: [{e: '1', date: Date.now()}], b: [{f: '2', date: Date.now()}], c: [{g: '3', date: Date.now()}], d: [{h: '4', date: Date.now()}] }
Arthur Rubens
  • 4,626
  • 1
  • 8
  • 11
1

You could use two nested reduce methods to create nested value of any depth level.

const a = { a: '1', b: '2', c: '3', d: '4' }
const b = { a: 'e.c.f', b: 'a', c: 'g', d: 'h', e: 'i', f: 'j', g: 'k' }

const result = Object.entries(a).reduce((r, [k, v]) => {
  if (k in b) {
    const value = [];

    b[k].split('.').reduce((a, e, i, arr) => {
      const value = arr[i + 1] ? [] : v
      const object = { [e]: value }

      if (!arr[i + 1]) {
        object.date = Date.now()
      }

      a.push(object)
      return arr[i + 1] ? value : a
    }, value)

    r[k] = value
  } else {
    r[k] = v
  }

  return r
}, {})

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

Similar to above answers, but might be easier to read for the user who asked the question.

Assuming you are using ES6, you can loop through an object using the Object.keys() function and .forEach.

Object.keys() will return an array of keys for an object, and you can call forEach() on that array.

var basis = { a: '1', b: '2', c: '3', d: '4' }

var nestedKeys = { a: 'e', b: 'f', c: 'g', d: 'h', e: 'i', f: 'j', g: 'k' }

Object.keys(basis).forEach(function (basisKey) {
  Object.keys(nestedKeys).forEach(function (matchedKey) {
    if (basisKey == matchedKey) {
      basis[basisKey] = [{
        [nestedKeys[matchedKey]] : basis[basisKey],
        'date' : Date.now(),
      }];
    }
  });
});

console.log(JSON.stringify(basis, null, 4));
Brent George
  • 108
  • 2
  • 11