gpt4 book ai didi

Javascript/AngularJS/NodeJS - 检查重复项?

转载 作者:太空宇宙 更新时间:2023-11-04 00:52:16 25 4
gpt4 key购买 nike

我有一个非常棘手的错误......

我将数据传递给函数。该函数要做的就是获取该数据并确保该数据不存在于另一个页面上。另一页上的数据是一个数组,如果其中没有任何内容,则给出未定义。我想做的事情的想法如下:

if($scope.profile.x.VALUE_IN_UNDEFINED_ARR_X === undefined)
{
//AKA theres nothing in the profile yet...add to users profile
}
else
{
if($scope.profile.x.name === myData.name)
{
//now theres a duplicate found
}
else
{
//add to users profile
}
}

我似乎无法让这样的事情发挥作用。对我来说,它总是执行第一个 if 语句(未定义)中的代码。然后它不断添加重复项。

我希望你们能帮我写一个循环来解决这个问题。

谢谢

最佳答案

使用typeof检查未定义

typeof $scope.profile.x.VALUE_IN_UNDEFINED_ARR_X == 'undefined'

此外,“配置文件中没有任何内容”可以解释为“$scope.profile 未定义”,如果是这样,您应该检查一下

typeof $scope.profile == 'undefined'

关于Javascript/AngularJS/NodeJS - 检查重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31870188/

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