首页 \ 问答 \ 即使元素JSON数组零长度?(JSON array zero length even with elements?)

即使元素JSON数组零长度?(JSON array zero length even with elements?)

我从服务器获取JSON响应并创建一个Javascript对象。 结构是这样的:

var response = {
    key1:[],
    key2:[],
    key3:[],
    key4:[],
    key5:[]
}

当请求完成时, response对象成功完成,如下所示:

Object (*expandable):
    key1: Array[0]
    key2: Array[0]
    key3: Array[0]
    key4: Array[20]
    key5: Array[113]

现在稍后我想将这些信息存储到数据库中。 我已经创建了一个函数,并且我使用了console.log响应对象来确保它没问题(这里变得有趣 - 请参阅注释):

function setupDatabase(){
    console.log(response); // prints the response correctly (see response above)
    console.log(response.key5); //prints key5: Array[0]. If I expand the Array[0] all the elements are inside.
    console.log("key5: "+response.key5.length);//prints 0!!
}

前3个键为0是正常的,因为没有为它们返回的元素。 其余2个都没问题。 为什么我会得到这个日志,而我在连续的同一个对象上运行3个console.log命令? 我错过了什么吗?


I am getting a JSON response from a server and creating a Javascript object. The structure is this:

var response = {
    key1:[],
    key2:[],
    key3:[],
    key4:[],
    key5:[]
}

When the request completes the response object is successfully completed like this:

Object (*expandable):
    key1: Array[0]
    key2: Array[0]
    key3: Array[0]
    key4: Array[20]
    key5: Array[113]

Now later on I want to store the information into a database. I have created a function and I console.log the response object to make sure it's ok (here it is getting interesting - see comments):

function setupDatabase(){
    console.log(response); // prints the response correctly (see response above)
    console.log(response.key5); //prints key5: Array[0]. If I expand the Array[0] all the elements are inside.
    console.log("key5: "+response.key5.length);//prints 0!!
}

It's normal for the first 3 keys to be 0 because there are no elements returned for them. The rest 2 are ok. Why do I get this log, while I run 3 console.log commands on the same object in a row? Am I missing something?


原文:https://stackoverflow.com/questions/30255173
更新时间:2022-04-05 14:04

最满意答案

不知道你为什么要这样做...但是使用itertuples

for row in df.itertuples():
    if row.check:
        print('Yes')

Yes
Yes
Yes
Yes
Yes
Yes
Yes

您可以使用map来映射您的值

df.check.map({True: 'Yes', False: ''})

0        
1        
2        
3     Yes
4        
5        
6        
7     Yes
8     Yes
9     Yes
10    Yes
11    Yes
12    Yes
Name: check, dtype: object

甚至assignassign回专栏

df.assign(mapped=df.check.map({True: 'Yes', False: ''}))

    Price  check mapped
0      10  False       
1      20  False       
2      30  False       
3      40   True    Yes
4      30  False       
5      20  False       
6      30  False       
7      40   True    Yes
8      50   True    Yes
9      60   True    Yes
10     70   True    Yes
11     80   True    Yes
12     90   True    Yes

您可以使用pd.DataFrame.where ,它在第一个参数的计算结果为True时保留值,在False时用第二个参数填充。 如果未传递第二个参数,则放置np.nan

df.Price.where(df.check, -99)

0    -99
1    -99
2    -99
3     40
4    -99
5    -99
6    -99
7     40
8     50
9     60
10    70
11    80
12    90
Name: Price, dtype: int64

姐妹方法是mask ,反之亦然。 保持第一个参数为False 。 这是一个等同的陈述

df.Price.mask(~df.check, -99)

0    -99
1    -99
2    -99
3     40
4    -99
5    -99
6    -99
7     40
8     50
9     60
10    70
11    80
12    90
Name: Price, dtype: int64

Not sure why you'd want to do this... but use itertuples

for row in df.itertuples():
    if row.check:
        print('Yes')

Yes
Yes
Yes
Yes
Yes
Yes
Yes

You can map your values with map

df.check.map({True: 'Yes', False: ''})

0        
1        
2        
3     Yes
4        
5        
6        
7     Yes
8     Yes
9     Yes
10    Yes
11    Yes
12    Yes
Name: check, dtype: object

And even assign it back to a column

df.assign(mapped=df.check.map({True: 'Yes', False: ''}))

    Price  check mapped
0      10  False       
1      20  False       
2      30  False       
3      40   True    Yes
4      30  False       
5      20  False       
6      30  False       
7      40   True    Yes
8      50   True    Yes
9      60   True    Yes
10     70   True    Yes
11     80   True    Yes
12     90   True    Yes

You can use pd.DataFrame.where which keeps the values when the first argument evaluates to True and fills in with the second argument when False. If the second argument isn't passed, np.nan is placed instead.

df.Price.where(df.check, -99)

0    -99
1    -99
2    -99
3     40
4    -99
5    -99
6    -99
7     40
8     50
9     60
10    70
11    80
12    90
Name: Price, dtype: int64

The sister method is mask which does the opposite. Keeps where the first argument is False. This is an equivalent statement

df.Price.mask(~df.check, -99)

0    -99
1    -99
2    -99
3     40
4    -99
5    -99
6    -99
7     40
8     50
9     60
10    70
11    80
12    90
Name: Price, dtype: int64

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。