3

I have an arrays of objects, lets say

[{a:'one',aa:'eleven'},{a:'two',aa:'twelve'},{a:'three',aa:'thirteen'}]

I want to convert the objects into arrays, something like this

[['one','eleven'], ['two','twelve'],['three','thirteen']]

as property name should map to a given correct index in the array

Is this possible using Lodash or javascript?

3
  • 1
    Does order matter? Commented Apr 17, 2017 at 17:31
  • It looks like order would matter so you would need some kind of map (property "a" goes into array slot 1, property "aa" goes into array slot 2, and so on) Commented Apr 17, 2017 at 17:32
  • That was what I was thinking. A simple sort may not be correct :) Commented Apr 17, 2017 at 17:34

3 Answers 3

5

If certain keys in the original objects must go to specific slots in the array, then you can create a map of key to slot number.

var data = [{
  a: 'one',
  aa: 'eleven'
}, {
  a: 'two',
  aa: 'twelve'
}, {
  a: 'three',
  aa: 'thirteen'
}];

var keyToSlot = {
  a: 0,
  aa: 1
};

var result = data.map(function(entry) {
  var keys = Object.keys(entry);
  return keys.reduce(function(acc, key) {
    var slot = keyToSlot[key];
    acc[slot] = entry[key];
    return acc;
  }, []);
});

console.log(result);

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

Comments

3

You can do this by grabbing the keys of every object, sorting them, and then mapping them to an array.

const arr = [{a:'one',aa:'eleven'},{a:'two',aa:'twelve'},{a:'three',aa:'thirteen'}]
const res = arr.map(x => Object.keys(x).sort().map(y => x[y]));

console.log(res)

Step by step:

  • arr.map iterates through each object
    • Object.keys(x).sort() gets the keys of the object and sorts them alphabetically.
    • .map(y => x[y]) on the above result turns the keys of the objects into the respective values.

3 Comments

but i also need the keys to be at particular index in the array, the keys will not be guranteed in same order
@ua_boaz the sort() call on the keys guarantees order does it not? You need to specify some way to sort the keys, and in this case I chose alphabetical.
@ua_boaz it's impossible to say to sort the object in the order that you wrote the keys in your code if that's what you want.
2

Here's a an approach using lodash#map with a lodash#unary wrapped lodash#map iteratee.

var result = _.map(data, _.unary(_.map));

var data = [{a:'one',aa:'eleven'},{a:'two',aa:'twelve'},{a:'three',aa:'thirteen'}];

var result = _.map(data, _.unary(_.map));

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Update To preserve key orders, you may replace the lodash#map iteratee into lodash#at that is partially applied with the keys that defines the order of the values.

var keys = ['aa', 'a'];
var result = _.map(data, _.unary(_.partialRight(_.at, keys)));

var data = [{a:'one',aa:'eleven'},{a:'two',aa:'twelve'},{a:'three',aa:'thirteen'}];

var keys = ['aa', 'a'];
var result = _.map(data, _.unary(_.partialRight(_.at, keys)));

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

An es6 alternative of the solution above would be:

var keys = ['aa', 'a'];
var result = _.map(data, v => _.at(v, keys));

var data = [{a:'one',aa:'eleven'},{a:'two',aa:'twelve'},{a:'three',aa:'thirteen'}];

var keys = ['aa', 'a'];
var result = _.map(data, v => _.at(v, keys));

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

1 Comment

How can we make sure we place certain key at a particular index

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.