在javascript中获取哈希表的信息

时间:2016-10-06 12:59:35

标签: javascript hashtable

快速提问 我有一个哈希表,每个键都有一个对象,它分为两部分:subject和query。 我试图获取查询和主题的值,但我不能。我得到的都是未定义的。 我如何获得价值?

###the top two lines are required on my linux machine
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
import numpy as np
from scipy.optimize import curve_fit #we could import more, but this is what we need
###defining your fitfunction
def func(x, a, b, c):
    return a - b* np.exp(c * x) 
###OP's data
baskets = np.array([475, 108, 2, 38, 320])
scaling_factor = np.array([95.5, 57.7, 1.4, 21.9, 88.8])
###let us guess some start values
initialGuess=[100, 100,-.01]
guessedFactors=[func(x,*initialGuess ) for x in baskets]
###making the actual fit
popt,pcov = curve_fit(func, baskets, scaling_factor,initialGuess)
#one may want to
print popt
print pcov
###preparing data for showing the fit
basketCont=np.linspace(min(baskets),max(baskets),50)
fittedData=[func(x, *popt) for x in basketCont]
###preparing the figure
fig1 = plt.figure(1)
ax=fig1.add_subplot(1,1,1)
###the three sets of data to plot
ax.plot(baskets,scaling_factor,linestyle='',marker='o', color='r',label="data")
ax.plot(baskets,guessedFactors,linestyle='',marker='^', color='b',label="initial guess")
ax.plot(basketCont,fittedData,linestyle='-', color='#900000',label="fit with ({0:0.2g},{1:0.2g},{2:0.2g})".format(*popt))
###beautification
ax.legend(loc=0, title="graphs", fontsize=12)
ax.set_ylabel("factor")
ax.set_xlabel("baskets")
ax.grid()
ax.set_title("$\mathrm{curve}_\mathrm{fit}$")
###putting the covariance matrix nicely
tab= [['{:.2g}'.format(j) for j in i] for i in pcov]
the_table = plt.table(cellText=tab,
                  colWidths = [0.2]*3,
                  loc='upper right', bbox=[0.483, 0.35, 0.5, 0.25] )
plt.text(250,65,'covariance:',size=12)
###putting the plot
plt.show()
###done

数据示例

function IntersectGroups(keyToGeneDetailMappingGroupArray) {

    allKeys = CreateSetWithAllKeys(keyToGeneDetailMappingGroupArray);

    var numElements = Math.pow(2, keyToGeneDetailMappingGroupArray.length);

    var results1= new Array(numElements);
    for (var j = 0; j < results.length; j++)
    {

        results1[j] = 0;
    }


    // Run for every key (for all groups)
    for (var currentKey in allKeys.items)
    {

        var linescontent="";
        var index = 0;

        // Run for every group
        for (var k = 0; k < keyToGeneDetailMappingGroupArray.length; k++)
        {
            var isGroupContained = keyToGeneDetailMappingGroupArray[k].hasItem(currentKey);

            if (isGroupContained)
            {
                //Not relevent: Watch the data structures: sumElements += keyToGeneDetailMappingGroupArray[k].getItem(currentKey).length;
                sumElements += 1; 
                linescontent += currentKey.Query;//I get undefined
                index += Math.pow(2, k);
            }
        }

        results1[index] += linescontent;
    }
    return results1;
}

enter image description here

2 个答案:

答案 0 :(得分:1)

您似乎要将自己的信息添加到results1,但是您将返回results,这将是未定义的。

答案 1 :(得分:0)

for (var currentKey in allKeys.items) {
    /* ... */
                linescontent += currentKey.Query;//I get undefined

for .. in将currentKey设置为allKeys.items对象(和原型)的键。即使它是一个数组,它也将被设置为项目的索引,而不是单个项目,并且将始终是一个字符串。

您的代码中没有信息可以告诉allKeys.items实际上是什么样的,但是如果您希望迭代这些元素,那么使用for (var currentKey of allKeys.items)(需要es2015环境)或{{1} }。