最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pick values of given properties from object into array - Stack Overflow

programmeradmin3浏览0评论

I have an object:

person = { 
    birth_year: 1970, 
    first_name: "John", 
    last_name: "Doe", 
    occupation: "Doctor", 
    city: "Boston",
    married: true 
}

I have an array of key names in given order:

keys = ["occupation", "last_name", "city"]

I want to get this array:

["Doctor", "Doe", "Boston"]

It is important, that the answer should guarantee the order (JavaScript does not guarantee the order for object iteration).

I think there is probably some utility function in lodash/underscore to do it simply, but can't figure out any.

I have an object:

person = { 
    birth_year: 1970, 
    first_name: "John", 
    last_name: "Doe", 
    occupation: "Doctor", 
    city: "Boston",
    married: true 
}

I have an array of key names in given order:

keys = ["occupation", "last_name", "city"]

I want to get this array:

["Doctor", "Doe", "Boston"]

It is important, that the answer should guarantee the order (JavaScript does not guarantee the order for object iteration).

I think there is probably some utility function in lodash/underscore to do it simply, but can't figure out any.

Share Improve this question edited Nov 9, 2016 at 16:26 Emile Bergeron 17.4k5 gold badges85 silver badges131 bronze badges asked Nov 9, 2016 at 12:08 Mikhail BatcerMikhail Batcer 2,0759 gold badges39 silver badges59 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

With Lodash you could use pick and values

var o = {
  birth_year: 1970,
  first_name: "John",
  last_name: "Doe",
  occupation: "Doctor",
  city: "Boston",
  married: true
}
var keys = ["occupation", "last_name", "city"];

var result = _.values(_.pick(o, keys));
console.log(result)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

You can use Array.prototype.map for this. Map loops through an array and creates a new array by applying a function to each item. When you use the keys array as a starting point, and return the value in o for that key, you'll get a new array with only values.

When using "dynamic" key names, you use a object[stringKeyName] notation to retrieve a value.

var o = { 
    birth_year: 1970, 
    first_name: "John", 
    last_name: "Doe", 
    occupation: "Doctor", 
    city: "Boston",
    married: true 
};

var keys = ["occupation", "last_name", "city"];

var result = keys.map(function(k) { return o[k]; });

console.log(result);

If it fits your style, you can create a helper method to replace the anonymous function:

var o = { birth_year: 1970, first_name: "John", last_name: "Doe", occupation: "Doctor", city: "Boston", married: true };
var keys = ["occupation", "last_name", "city"];

var prop = obj => key => obj[key];
var result = keys.map(prop(o));

console.log(result);

For those who stumble on this answer years later. You can use lodash at method

// I have an object:
const person = { 
    birth_year: 1970, 
    first_name: "John", 
    last_name: "Doe", 
    occupation: "Doctor", 
    city: "Boston",
    married: true 
}

// I have an array of key names in given order:
const keys = ["occupation", "last_name", "city"]

// I want to get this array:
// ["Doctor", "Doe", "Boston"]
const result = at(person, ["occupation", "last_name", "city"])
发布评论

评论列表(0)

  1. 暂无评论