Node.js 中 ES5 过滤器的奇怪行为
Strange behaviour with ES5 filter in Node.js
我正在尝试使用 ES5 过滤以下数据:过滤器。
{
"EmailAddress": "joe@example.com",
"Name": "",
"Date": "2009-01-23 06:22:00",
"State": "Active",
"CustomFields": [
{
"Key": "[FirstName1]",
"Value": "joe"
},
{
"Key": "[LastName1]",
"Value": "bloggs"
}
]
}
为了获得第一个 Name ,我尝试了以下代码:
const firstName = subscriber.CustomFields.filter(cf => cf.Key='FirstName1')
但是,在那行代码之后,源更改为(请参阅 LastName1 更改为 FirstName1):
{
"EmailAddress": "joe@example.com",
"Name": "",
"Date": "2009-01-23 06:22:00",
"State": "Active",
"CustomFields": [
{
"Key": "[FirstName1]",
"Value": "joe"
},
{
"Key": "[FirstName1]",
"Value": "bloggs"
}
]
}
我不明白通过过滤器提取数据如何改变源。我是不是忽略了一些基本的东西?
您应该使用 ==
而不是赋值 (=
)。
const firstName = subscriber.CustomFields.filter(cf => cf.Key == 'FirstName1')
let subscriber = { "EmailAddress": "joe@example.com", "Name": "", "Date": "2009-01-23 06:22:00", "State": "Active", "CustomFields": [ { "Key": "[FirstName1]", "Value": "joe" }, { "Key": "[LastName1]", "Value": "bloggs" } ] }
const firstName = subscriber.CustomFields.filter(cf => cf.Key == '[FirstName1]');
console.log(firstName);
我正在尝试使用 ES5 过滤以下数据:过滤器。
{
"EmailAddress": "joe@example.com",
"Name": "",
"Date": "2009-01-23 06:22:00",
"State": "Active",
"CustomFields": [
{
"Key": "[FirstName1]",
"Value": "joe"
},
{
"Key": "[LastName1]",
"Value": "bloggs"
}
]
}
为了获得第一个 Name ,我尝试了以下代码:
const firstName = subscriber.CustomFields.filter(cf => cf.Key='FirstName1')
但是,在那行代码之后,源更改为(请参阅 LastName1 更改为 FirstName1):
{
"EmailAddress": "joe@example.com",
"Name": "",
"Date": "2009-01-23 06:22:00",
"State": "Active",
"CustomFields": [
{
"Key": "[FirstName1]",
"Value": "joe"
},
{
"Key": "[FirstName1]",
"Value": "bloggs"
}
]
}
我不明白通过过滤器提取数据如何改变源。我是不是忽略了一些基本的东西?
您应该使用 ==
而不是赋值 (=
)。
const firstName = subscriber.CustomFields.filter(cf => cf.Key == 'FirstName1')
let subscriber = { "EmailAddress": "joe@example.com", "Name": "", "Date": "2009-01-23 06:22:00", "State": "Active", "CustomFields": [ { "Key": "[FirstName1]", "Value": "joe" }, { "Key": "[LastName1]", "Value": "bloggs" } ] }
const firstName = subscriber.CustomFields.filter(cf => cf.Key == '[FirstName1]');
console.log(firstName);