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

javascript - Lodash get key values from array of objects - Stack Overflow

programmeradmin3浏览0评论

I have an array of objects,

var out = [{
    "type": "1",
    "from": "13052033555",
    "to": "4444444",
    "amount": "40000",
    "date": 1461575799,
    "status": "1"
  }, {
    "type": "2",
    "from": "13052033555",
    "to": "1111111",
    "amount": "30000",
    "date": 1461575884,
    "status": "1"
  }...
];

I get only it's values without keys

Now i used this function to get the values from array like this, I pass array then it returns only values without keys

function foo(a) {
  var values = [];
  for (var i = 0; i < a.length; i++) {
    var obj = a[i];
    var arr = Object.keys(obj).map(function(k) {
      return obj[k]
    });
    values.push("[" + arr + "],");
  }
  return values.join('');
}

Then it returns the values data without keys like this,

[ ["1","13052033555","4444444","40000",1461575799,"1"],
  ["2","13052033555","1111111","30000",1461575884,"1"],
    ....]

Question: How can i change my foo function to lodash?

I have an array of objects,

var out = [{
    "type": "1",
    "from": "13052033555",
    "to": "4444444",
    "amount": "40000",
    "date": 1461575799,
    "status": "1"
  }, {
    "type": "2",
    "from": "13052033555",
    "to": "1111111",
    "amount": "30000",
    "date": 1461575884,
    "status": "1"
  }...
];

I get only it's values without keys

Now i used this function to get the values from array like this, I pass array then it returns only values without keys

function foo(a) {
  var values = [];
  for (var i = 0; i < a.length; i++) {
    var obj = a[i];
    var arr = Object.keys(obj).map(function(k) {
      return obj[k]
    });
    values.push("[" + arr + "],");
  }
  return values.join('');
}

Then it returns the values data without keys like this,

[ ["1","13052033555","4444444","40000",1461575799,"1"],
  ["2","13052033555","1111111","30000",1461575884,"1"],
    ....]

Question: How can i change my foo function to lodash?

Share Improve this question asked Jul 9, 2016 at 16:54 Ceddy MuhozaCeddy Muhoza 6363 gold badges18 silver badges34 bronze badges 2
  • out.map(obj => _.values(obj)) – gcampbell Commented Jul 9, 2016 at 16:58
  • Other solution out.map((e)=>Object.keys(e).map((key)=>e[key])) – Nenad Vracar Commented Jul 9, 2016 at 17:00
Add a comment  | 

2 Answers 2

Reset to default 15

Use _.values()

    var out = [{
        "type": "1",
        "from": "13052033555",
        "to": "4444444",
        "amount": "40000",
        "date": 1461575799,
        "status": "1"
      }, {
        "type": "2",
        "from": "13052033555",
        "to": "1111111",
        "amount": "30000",
        "date": 1461575884,
        "status": "1"
      }
    ];

    for (var i = 0; i < out.length; i++) {
      out[i] = _.values(out[i]);
    }
    

console.log(out)
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>

out.map(_.values);

Or if below ES5: _.map(out, _.values);

发布评论

评论列表(0)

  1. 暂无评论