首先先安裝lodash
安裝方法
sudo npm install lodash
[sudo] password for sh:
npm WARN saveError ENOENT: no such file or directory, open '/home/sh/test/node/01/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/home/sh/test/node/01/package.json'
npm WARN 01 No description
npm WARN 01 No repository field.
npm WARN 01 No README data
npm WARN 01 No license field.
+ lodash@4.17.14
updated 1 package and audited 1 package in 0.844s
found 0 vulnerabilities
╭────────────────────────────────────────────────────────────────╮
│ │
│ New minor version of npm available! 6.9.0 → 6.10.1 │
│ Changelog: https://github.com/npm/cli/releases/tag/v6.10.1 │
│ Run npm install -g npm to update! │
│ │
╰────────────────────────────────────────────────────────────────╯
這樣就裝好了
然後寫下面這段程式碼
const _ = require('lodash');
var rows = {};
rows = [{"id":405,"client_id":62,"email":"sh@gmail.com","client":{"client_id":"dev"}},{"id":406,"client_id":26,"email":"sh@gmail.com","client":{"client_id":"skill"}},{"id":407,"client_id":62,"email":"sh@gmail.com","client":{"client_id":"test"}}];
let ids = [];
_.forEach(rows, (obj) => {
ids.push(obj.client.client_id);
});
console.log(ids);
最後的結果
[ 'dev', 'skill', 'test' ]
想對外分享這則貼文嗎?運用網址更方便呦~