ukaric
ukaric

Reputation: 428

Pull key from Array of Objects Dynamically And Transform to Array of Objects

I have a specific case and I don't even know if it is possible to achieve.

Given the input array.

var originalArr = [
  [
    { ID: 3, name: 'Beef' },
    { ID: 4, name: 'Macaroni' },
    { ID: 5, name: 'Sauce#1' }
  ],
  [{ ID: 1, name: 'Lettuce' }, { ID: 2, name: 'Brocoli' }]
];

I would like to iterate over the inner arrays and pick the ID's from objects then create new one in place of array. So my output should look something like this.

var output = [
  {
    '1': {
      name: 'Lettuce',
      ID: 1
    },
    '2': {
      name: 'Brocoli',
      ID: 2
    }
  },
  {
    '3': {
      name: 'Beef',
      ID: 3
    },
    '4': {
      name: 'Macaroni',
      ID: 4
    },
    '5': {
      name: 'Sauce#1'
    }
  }
];

It is easy to iterate over the inner arrays with map but how can I create new Object in place of the array and have its key value dynamically pulled up ? And is it even possible given my input to produce the desired output.

Upvotes: 0

Views: 30

Answers (3)

Damilola
Damilola

Reputation: 241

const original_array = [
[
    { ID: 3, name: 'Beef' },
    { ID: 4, name: 'Macaroni' },
    { ID: 5, name: 'Sauce#1' }
],
[
    { ID: 1, name: 'Lettuce' },
    { ID: 2, name: 'Brocoli' }
]
]

let new_array = []

for (let i=0; i < original_array.length; i++) {
    if (original_array[i + 1]) new_array = 
    new_array.concat(original_array[i].concat(original_array[i+1]))
}

let output = []

for (let i=0; i<new_array.length; i++) {
    output.push({[new_array[i].ID]: new_array[i]})
}

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

Use map and reduce

originalArr.map( s => //iterate outer array
    s.reduce( (acc, c) => (  //iterate inner array using reduce
       acc[c.ID] = c, acc  //assign the id as key to accumulator and return the accumulator
    ) , {}) //initialize accumulator to {}
)

Demo

var originalArr = [
  [
    { ID: 3, name: 'Beef' },
    { ID: 4, name: 'Macaroni' },
    { ID: 5, name: 'Sauce#1' }
  ],
  [{ ID: 1, name: 'Lettuce' }, { ID: 2, name: 'Brocoli' }]
];

var output = originalArr.map( s => s.reduce( (acc, c) => ( acc[c.ID] = c, acc ) , {}) );

console.log(output);

Upvotes: 4

Birbal Singh
Birbal Singh

Reputation: 1072

You can achieve using recursion with pure javascript

var originalArr = [
        [{
            ID: 3,
            name: 'Beef'
        }, {
            ID: 4,
            name: 'Macaroni'
        }, {
            ID: 5,
            name: 'Sauce#1'
        }],
        [{
            ID: 1,
            name: 'Lettuce'
        }, {
            ID: 2,
            name: 'Brocoli'
        }]
    ]

    function bindInObject(object, array) {
        for (var i = 0; i < array.length; i++) {
            var it = array[i];
            if (it instanceof Array) {
                bindInObject(object, it);
            } else {
                var id = it.ID;
                object[id] = it;
            }
        }
    }
    var output = {};

    bindInObject(output, originalArr);
    console.log(output) 

Upvotes: 1

Related Questions