0

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.

2
  • One question: in your output, the object with ID values "1" and "2" is first, but in the input array it came second. Is there some sorting you want to do in addition to the rearrangement? Commented Mar 19, 2018 at 13:36
  • No need for stuff to be in order. Commented Mar 19, 2018 at 13:37

3 Answers 3

4

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);

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

Comments

1

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) 

Comments

0
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]})
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.