gpt4 book ai didi

javascript - Node.js 中 ES5 过滤器的奇怪行为

转载 作者:行者123 更新时间:2023-11-30 20:17:34 28 4
gpt4 key购买 nike

我正在尝试使用 ES5 过滤下面的数据:filter。

{
"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);

关于javascript - Node.js 中 ES5 过滤器的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51781575/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com