Lodash从选定键创建新数组

时间:2018-07-10 12:31:07

标签: javascript reactjs lodash

我的道具数据输出为:

const { profile } = this.props;
console.log("help", profile);

{notes: "", name: "John", gender: "M", dob: "", age: "33", nationality: "British", notes2: "no notes", notes3: "no notes"}

我希望能够提取笔记并最终得到如下结果:

{notes: "", notes2: "no notes", notes3: "no notes"}

我需要保留“个人资料”作为其自身的道具,因此需要创建一个const来存储此输出。

我尝试过这样的事情:

const oldArray = _.map(_.toPairs(profile), d => _.fromPairs([d]));
const newArray = _.map(oldArray => [{notes: notes}, {notes: notes2}, {notes: notes3}]);

我要执行此操作的原因是,然后运行检查以查看所有音符是否为空,但是当它们位于具有许多不同键的数组中时,则无法执行此操作。

4 个答案:

答案 0 :(得分:1)

如果有一个对象,则可以使用const obj = {notes: "", name: "John", gender: "M", dob: "", age: "33", nationality: "British", notes2: "no notes", notes3: "no notes"} const notes = _.pick(obj, ['notes', 'notes2', 'notes3']); console.log(notes)方法并返回具有特定属性的新对象。

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(X, Y, Z, c=Value, lw=0, s=20)
plt.show()

答案 1 :(得分:0)

您可以遍历该对象的键,并检查键是否以notes作为子字符串。如果是,则将key-value添加到新对象。使用此功能,您不必担心诸如notes1notes2notes3等键之类的问题。您只需检查子字符串notes

var obj = {notes: "", name: "John", gender: "M", dob: "", age: "33", nationality: "British", Notes2: "no notes", notes3: "no notes"};
var res = {};
Object.keys(obj).forEach((key) => {
  if(key.toLowerCase().indexOf('notes') !== -1){
    res[key] = obj[key];
  }
});
console.log(res);

答案 2 :(得分:0)

您可以使用普通js代替lodash:

undefined

然后您可以检查值:

<table class="ms-rteTable-0 " cellspacing="0" cellpadding="0" style="width: 15%; height: 198px;">
   <tbody>
      <tr class="ms-rteTableEvenRow-0">
         <td class="ms-rteTableEvenCol-0" style="width: 30px; height: 16px;">​<a title="" href="" target="_blank"><img src="" alt="" style="margin: 0px; width: 60px; height: 59px;"/></a></td>
         <td class="ms-rteTableOddCol-0" style="width: 20px; height: 16px;">​<a title="" href="/" target="_blank"><img src="" alt="" style="margin: 0px; width: 60px; height: 59px;"/></a></td>
         <td class="ms-rteTableEvenCol-0" style="width: 30px; height: 16px;">​<a title="" href="" target="_blank"><img src="" _moz_resizing="true" alt="" style="margin: 0px; width: 60px; height: 59px;"/></a></td>
      </tr>
      <tr class="ms-rteTableOddRow-0">
         <td class="ms-rteTableEvenCol-0" rowspan="1" style="width: 30px; height: 76px;">​<a title="" href="" target="_blank"><img src="" alt="" style="margin: 0px; width: 60px; height: 59px;"/></a></td>
         <td class="ms-rteTableOddCol-0" rowspan="1" style="width: 20px; height: 76px;">​</td>
         <td class="ms-rteTableEvenCol-0" rowspan="1" style="width: 30px; height: 76px;">​<a title="" href="" target="_blank"><img src="" alt="" style="margin: 0px; width: 60px; height: 59px;"/></a></td>
      </tr>
   </tbody>
</table>

答案 3 :(得分:0)

_.pickBy()String.startsWith()(或lodash's equivalent)一起使用以查找以 notes 单词开头的所有键:

const props = {notes: "", name: "John", gender: "M", dob: "", age: "33", nationality: "British", notes2: "no notes", notes3: "no notes"};

const notes = _.pickBy(props, (v, k) => k.startsWith('notes'));

console.log(notes)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>