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

javascript - Lodash split object - Stack Overflow

programmeradmin2浏览0评论

how can i use lodash to split the following object into two arrays

the object is

{"M":
  [
   "Alpha",
   "Beta",
   "Zeta"],
 "F":
    [
     "Alpha",
     "Omega"
  ]}

I want it to be

var first =  [
   "Alpha",
   "Beta",
   "Zeta"] // contents of M

var second =   [
     "Alpha",
     "Omega"
  ] // contents of F

is that possible in lodash? and if not then how is it possible in vanillaJS

thanks

how can i use lodash to split the following object into two arrays

the object is

{"M":
  [
   "Alpha",
   "Beta",
   "Zeta"],
 "F":
    [
     "Alpha",
     "Omega"
  ]}

I want it to be

var first =  [
   "Alpha",
   "Beta",
   "Zeta"] // contents of M

var second =   [
     "Alpha",
     "Omega"
  ] // contents of F

is that possible in lodash? and if not then how is it possible in vanillaJS

thanks

Share Improve this question edited Jul 13, 2016 at 0:03 user3052526 asked Jul 12, 2016 at 13:04 user3052526user3052526 68310 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You don't need Lodash for this. Just:

var first = theObject.M;
var second = theObject.F;

first and second will refer to the arrays in the object. If you want to copy them, then:

var first = theObject.M.slice();
var second = theObject.F.slice();

Just for reference, this can be done very easily in es6:

const { M: first, F: second } = theObject;

发布评论

评论列表(0)

  1. 暂无评论