如何仅从对象数组中过滤特定数据

时间:2017-10-08 19:11:29

标签: javascript

我只需要从中提取特定数据。例如:移动号码:

这只是控制台的结果:
Result from the console

我应该在JS上写什么代码。

3 个答案:

答案 0 :(得分:0)

如果是一个对象,您可以通过在点后面调用其名称(例如myObject.mobile)或括号语法来访问其属性:myObject['mobile']

您需要知道的所有内容:https://www.w3schools.com/js/js_properties.asp

如果它是一个集合,只需使用forEachmap函数(或任何其他可用方法)迭代它,然后访问属性,如上所示。

答案 1 :(得分:0)

您可以使用库LoDash。将库添加到项目中并使用.pick方法。如果您的数据是数组,则使用.map方法

var data = {
var1: 1,
var2: 2,
var3: 3
};

console.log(data);
var mappedData = _.pick(data,  'var2' );
console.log(mappedData);

答案 2 :(得分:0)

如果您的数据是对象,

Private Sub CancelButton_Click()

 Unload Me

End Sub

Private Sub ClearButton_Click()

Call UserForm_Initialize

End Sub

Private Sub InitialTermListBox_Click()

End Sub


Private Sub OKButton_Click()
Dim emptyRow As Long

'Make Summary active
 Summary.Activate

 'Determine emptyRow
 emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
 Cells(emptyRow, 1).Value = SupplierNameTextBox.Value
 Cells(emptyRow, 3).Value = GeneralDescriptionTextBox.Value
 Cells(emptyRow, 5).Value = DepartmentListBox.Value
 Cells(emptyRow, 6).Value = ContractStartDateTextBox.Value
 Cells(emptyRow, 7).Value = InitialTermListBox.Value
 Cells(emptyRow, 8).Value = RenewalTermListBox.Value
 Cells(emptyRow, 9).Value = PaymentTermsListBox.Value
 Cells(emptyRow, 10).Value = SelectionMechanismListBox.Value
 Cells(emptyRow, 11).Value = ValueOfContractTextBox.Value

 Dim lbtext As Variant
 lbtext = BusinessOwnerListBox.Value
 Worksheets("Summary").Cells(emptyRow, 4).Value = lbtext


If SignedContractCheckBox.Value = True Then Cells(emptyRow, 2).Value =        SignedContractCheckBox.Caption

End Sub


Private Sub PaymentTermsListBox_Click()

End Sub

Private Sub RenewalTermListBox_Click()

End Sub

Private Sub SelectionMechanismListBox_Click()

End Sub

Private Sub UserForm_Click()

 End Sub

Private Sub UserForm_Initialize()

'Empty SupplierNameTextBox
 SupplierNameTextBox.Value = ""

'Uncheck SignedContractCheckBox
 SignedContractCheckBox.Value = False

'Empty GeneralDescriptionTextBox
 GeneralDescriptionTextBox.Value = ""

 'Empty BusinessOwnerListBox
 BusinessOwnerListBox.Clear

 'Fill BusinessOwnerListBox
With BusinessOwnerListBox
    .AddItem ""
    .AddItem "Alison Gillies"
    .AddItem "Bernard Hunwick"
    .AddItem "Jon Williams"
    .AddItem "Laurent Sylvestre"
    .AddItem "Leeann McCallum"
    .AddItem "Sue Lowe"
End With

 'Empty DepartmentListBox
 DepartmentListBox.Clear

'Fill DepartmentListBox
 With DepartmentListBox
 .AddItem ""
 .AddItem "Buildings"
 .AddItem "Corporate Services"
 .AddItem "ICT"
 .AddItem "People & Culture"
 .AddItem "Transport & Logistics"
 End With

'Empty ContractStartDateTextBox
  ContractStartDateTextBox.Value = ""

 'Empty InitialTermListBox
  InitialTermListBox.Clear

 'Fill InitialTermListBox
 With InitialTermListBox
 .AddItem ""
 .AddItem "6"
 .AddItem "12"
 .AddItem "18"
 .AddItem "24"
 .AddItem "36"
End With

 'Empty RenewalTermListBox
  RenewalTermListBox.Clear

 'Fill RenewalTermListBox
  With RenewalTermListBox
 .AddItem ""
 .AddItem "6"
 .AddItem "12"
 .AddItem "18"
 .AddItem "24"
 .AddItem "36"
 End With

'Empty PaymentTermsListBox
 PaymentTermsListBox.Clear

'Fill PaymentTermsListBox
 With PaymentTermsListBox
 .AddItem ""
 .AddItem "7 days"
 .AddItem "30 days"
 .AddItem "20th month"
 .AddItem "Quarterly"
 .AddItem "Annual"
 End With

'Empty SelectionMechanismListBox
 SelectionMechanismListBox.Clear

'Fill SelectionMechanismListBox
 With SelectionMechanismListBox
 .AddItem ""
 .AddItem "RolledContract"
 .AddItem "RFP"
 .AddItem "RFQ"
 .AddItem "3 Quotes"
 .AddItem "2 Quote"
 .AddItem "Business Selection"
 End With

'Empty ValueOfContractTextBox
  ValueOfContractTextBox.Value = ""


 'Set Focus on SupplierNameTextBox
  SupplierNameTextBox.SetFocus


 End Sub

如果您的数据是一个对象数组,

var data = { entryId: 'ABC', mobile: '0123456789' } 
console.log(data.entryId)  // 'ABC'
console.log(data.mobile)  // '0123456789'

有关map()方法的详细信息,请访问https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map