1
order = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto'];

var scrambled = {
  pluto : 'pluto very far',
  mars: 'mars very near',
  saturn: 'saturn dnt care',
  jupiter: 'jupiter',
  uranus : 'uranus',
  earth: 'earth',
  mercury: 'mercury',
  venus: 'venus',
  neptune: 'neptune'
};

need to extract the object into an array with the given order,

I am looking for a solution which can solve this is max of 2 to 3 lines.

2
  • 2
    "how to get the array with a given order" The array already is in the right order, isn't it? You have it already. There is nothing to get :) order.map(x => scrambled[x]). Commented Sep 20, 2016 at 18:31
  • Is that supposed to be an array of strings? Commented Sep 20, 2016 at 18:33

3 Answers 3

3

There is no need of any external library use native JavaScript Array#map method.

// iterate over order array
var res = order.map(function(k) {
  // generate array element based 
  // on the order array element
  return scrambled[k];
});

// with ES6 arrow function

var res = order.map(k => scrambled[k]);

order = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto'];

var scrambled = {
  pluto: 'pluto very far',
  mars: 'mars very near',
  saturn: 'saturn dnt care',
  jupiter: 'jupiter',
  uranus: 'uranus',
  earth: 'earth',
  mercury: 'mercury',
  venus: 'venus',
  neptune: 'neptune'
};

var res = order.map(function(k) {
  return scrambled[k];
});

console.log(res);

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

Comments

1

You can use lodash's _.at():

var order = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto'];

var scrambled = {
  pluto : 'pluto very far',
  mars: 'mars very near',
  saturn: 'saturn dnt care',
  jupiter: undefined,
  uranus : 'uranus',
  earth: 'earth',
  mercury: 'mercury',
  venus: 'venus',
  neptune: 'neptune'
};

var result = _.at(scrambled, order);

console.log('with undefineds', result);

var result = _.filter(_.at(scrambled, order), _.negate(_.isUndefined)); // filter out all undefined items

console.log('without undefineds', result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>

2 Comments

In cases where scrambled has missing fields that might show as undefined in the array. here is a soln to that var res = .remove(.at(scrambled, order), function(n){ return !_.isUndefined(n); });
I prefer this - _.filter(_.at(scrambled, order), _.negate(_.isUndefined));. Added it to answer.
0

Pure JS this should do it

var order = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto'],
scrambled = {
  pluto : 'pluto very far',
  mars: 'mars very near',
  saturn: 'saturn dnt care',
  jupiter: 'jupiter',
  uranus : 'uranus',
  earth: 'earth',
  mercury: 'mercury',
  venus: 'venus',
  neptune: 'neptune'
},
   sorted = order.map(k => scrambled[k]);
console.log(sorted);

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.