KnockoutJS Mapping toJS忽略嵌套键(KnockoutJS Mapping toJS ignore nested keys)

在KnockoutJS的映射插件中使用toJS时,我不能忽略嵌套属性吗?

例:

var obj = {
    "akey": {
        "anestedkey": "ignore",
        "anotherkey": "value"
    }
};

console.log(ko.mapping.toJS(obj, {
    "ignore": ["akey.anestedkey"],
}));

预期的产出是

{
    akey: {
        anotherkey: "value"   
    }
}

实际输出是

{
    akey: {
        anestedkey: "ignore"
        anotherkey: "value"   
    }
}

JSFiddle: http//jsfiddle.net/48KVU/


Can I not ignore nested properties when using toJS in KnockoutJS' mapping plugin?

Example:

var obj = {
    "akey": {
        "anestedkey": "ignore",
        "anotherkey": "value"
    }
};

console.log(ko.mapping.toJS(obj, {
    "ignore": ["akey.anestedkey"],
}));

Expected output is

{
    akey: {
        anotherkey: "value"   
    }
}

Actual output is

{
    akey: {
        anestedkey: "ignore"
        anotherkey: "value"   
    }
}

JSFiddle: http://jsfiddle.net/48KVU/


原文:https://stackoverflow.com/questions/24326358
2022-11-09 21:11

满意答案

您不需要第二个数据帧dfData。 您需要的所有数据都在dfTmp中。 您只需使用以下内容重塑dfTmp:

选项1

使用set_indexstack

dfData = dftmp.rename_axis('Year', 1).set_index('City').stack().reset_index(name='Value')

print(dfData)

输出:

     City  Year  Value
0  London  2005      3
1  London  2007      7
2   Paris  2005      2
3   Paris  2007      0

选项2

使用melt

dfData = dftmp.melt(id_vars='City', var_name='Year', value_name='Value')

print(dfData)

输出:

     City  Year  Value
0  London  2005      3
1   Paris  2005      2
2  London  2007      7
3   Paris  2007      0

You don't need the second dataframe, dfData. All the data you need is in the dfTmp. You just need to reshape dfTmp using the following:

Option 1

using set_index and stack

dfData = dftmp.rename_axis('Year', 1).set_index('City').stack().reset_index(name='Value')

print(dfData)

Output:

     City  Year  Value
0  London  2005      3
1  London  2007      7
2   Paris  2005      2
3   Paris  2007      0

Option 2

using melt:

dfData = dftmp.melt(id_vars='City', var_name='Year', value_name='Value')

print(dfData)

Output:

     City  Year  Value
0  London  2005      3
1   Paris  2005      2
2  London  2007      7
3   Paris  2007      0

相关问答

更多

R合并列上的两个数据帧保留列(R Merge Two Dataframes on columns keep columns)

由于合并列在内部联接或数据框之间完全匹配时完全相同,因此只需将新列分配给剩余的列。 你可以用transform()来做到这一点。 下面添加了一个带paste0的outer()组合来检索所需的列顺序: matchkey <- transform(merge(df1, df2, by.x = c("fname1", "sname1"), by.y = c("fname2", "sname2")), ...

将两个DataFrame合并为一些相等的列(Merge two DataFrames with some equal columns)

通常你可以用适当的索引解决这个问题: df1.set_index(['id', 'noteId'], inplace=True) df1.update(df2) (如果你之后不想要那个索引,只需要df1.reset_index(inplace=True) ) Usually you can solve this with the proper index: df1.set_index(['id', 'noteId'], inplace=True) df1.update(df2) (And if...

比较并合并两个数据帧(Compare and merge two dataframes)

library(dplyr) df1 = data.frame(c("A", "A", "A", "B", "B"), c(1, 11, 21, 35, 45), c(6, 20, 30, 40, 60), c(1, 2, 3, 4, 5), stringsAsFactors = F) colnames(df1) = c("X", "Y", "Z", "score") df2 = data.frame(c("A", "A", "A", "A", "B", "...

合并DataFrames与来自两个不同列的匹配值 - 熊猫(Merge DataFrames with Matching Values From Two Different Columns - Pandas [duplicate])

在pd.merge使用how='inner' : merged_df = DF2.merge(DF1, how = 'inner', on = ['date', 'hours']) 这将执行和“内部连接”,从而省略每个数据帧中不匹配的行。 因此,合并数据帧的右侧或左侧没有NaN。 Use how='inner' in pd.merge: merged_df = DF2.merge(DF1, how = 'inner', on = ['date', 'hours']) This will per...

基于日期合并两个数据帧时出错(Errors when merging two dataframes based on date)

它似乎是“日期”的类型,其中“2013-8-1”与“2013-08-1”不同。 在合并它们之前: df1$date = as.Date(df1$date); df2$date = as.Date(df2$date); It seems the type of "date", where "2013-8-1" is different from "2013-08-1". before merge them: df1$date = as.Date(df1$date); df2$date = as.D...

如果索引和长度都不匹配,如何合并两个数据帧?(how to merge two dataframes if the index and length both do not match?)

您可以将reset_index与drop=True ,将索引重置为默认整数索引。 pd.concat([df_1.reset_index(drop=True), df_2.reset_index(drop=True)], axis=1) A B C D 0 1 2 3 10.0 1 4 5 6 11.0 2 7 8 9 12.0 3 Nan Nan Nan NaN You could use r...

Pandas:连接数据帧并合并相同列的值(Pandas: join dataframes and merge values of identical columns)

在数据pd.merge的长度或绝对数量很大时,在循环中调用pd.merge会导致二次复制和性能降低。 所以尽可能避免这种情况。 在这里,似乎我们想要在Time和Filter_type列时垂直连接DataFrames,并且我们希望在DataFrames缺少Filter_type列时水平连接: frames = [df.set_index('Time') for df in frames] filter_type_frames = pd.concat(frames[:4], axis=0) resul...

如何根据标题和列值合并两个数据帧?(How to merge two dataframes based on header and columns values?)

您不需要第二个数据帧dfData。 您需要的所有数据都在dfTmp中。 您只需使用以下内容重塑dfTmp: 选项1 使用set_index和stack dfData = dftmp.rename_axis('Year', 1).set_index('City').stack().reset_index(name='Value') print(dfData) 输出: City Year Value 0 London 2005 3 1 London 2007 ...

根据行中的值合并pandas数据帧(Merge pandas dataframes based on value in row)

我认为需要: df4 = (df2.reset_index().pivot('index','x','y') .rename_axis('b') .reset_index() .merge(df1, on='b') .rename(columns={True:'y_x', False:'y_notx'})) print (df4) b y_notx y_x 0 3 6.9 5.4 1 4 NaN...

如何根据所选列中的值连接两个数据帧?(How do I join two dataframes based on values in selected columns?)

更新:(用于更新的DF和新的所需DF) In [286]: merged = pd.merge(df1, df2, on=['A','B'], how='outer', suffixes=('','_y')) In [287]: merged.L.fillna(merged.pop('L_y'), inplace=True) In [288]: merged Out[288]: A B C D L E F 0 4 3 1.0 5.0 1.0 4...

相关文章

更多

elasticsearch 口水篇(6) Mapping 定义索引

前面我们感觉ES就想是一个nosql数据库,支持Free Schema。 接触过Lucene、solr ...

Hibernate Search(基于version3.4)--第四章Mapping entities to the index structure

Mapping entities to the index structure 4.1. 映射一个实 ...

ElasticSearch入门-Get Mapping

想要在Java API中获得一个mapping 还真困难,以此铭记。 import org.elast ...

ElasticSearch入门-结构定义之Mapping

相当于数据库的表结构的定义,elasticsearch的mapping 也很重要。直接关系到性能及搜索 ...

My W3C Custom Mapping File

[hdhw] HotKey=W Tip=Train Dragonha|cffffcc00w|r ...

GitHub创建SSH Keys

GitHub创建SSH Keys 分类: GitHub ...

About Unity3D 4.1.2 (to continue…)

Here are something that need to take care of when y ...

spring 嵌套事务 的 加锁 问题

有两个service:ServiceA 和 ServiceB ServiceA { metho ...

Hibernate 保存数据时异常?

首先,这是我的hbm.xml &lt;?xml version=&quot;1.0&quot; en ...

hibernate添加删除时的主外键问题

有两张表:成绩表和课程表。课程表的主键CourseId是成绩表的外键。现在我要添加一个成绩,在jsp页 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本