我有一个数组表示法的objects数组。这个数组每次都会有所不同。
我希望能够获得此数组中元素的名称。我遇到的麻烦是返回[object]而不是返回assetA,例如。我知道这是因为assetA本身就是一个对象。
我用来测试它的代码示例是..
//the assetArray will have varying number of elements in the future
var assetArray=[assetX,assetY,assetZ, assetB, assetA];
var testtest=(new String(assetArray));
alert(testtest);
$.each(assetArray,function(intIndex,objValue){
var test123=(new String(assetArray[intIndex]));
alert(test123);
});
并且测试资产列表是..
var assetX = {
assetNumber: "TESTX",
assetDescription: "FLUX CAPACITOR",
assetManufacturer: "Honeywell",
assetCustomer: "MCFLY",
assetDate: "03/04/1956"
};
var assetY = {
assetNumber: "C123Y",
assetDescription: "HOVERBOARD",
assetManufacturer: "GE",
assetCustomer: "MCFLY",
assetDate: "12/03/1945"
};
var assetZ = {
assetNumber: "9000Z",
assetDescription: "ROCKETFOOTBALL",
assetManufacturer: "Fluke",
assetCustomer: "MCFLY JR.",
assetDate: "01/05/3030"
};
var assetA = {
assetNumber: "C34JJXA",
assetDescription: "TEST DESCRIPTION",
assetManufacturer: "Elgar",
assetCustomer: "CUSTOMER1",
assetDate: "05/09/1923"
};
var assetB = {
assetNumber: "C892ALB",
assetDescription: "DMM",
assetManufacturer: "Agilent",
assetCustomer: "CUSTOMER2",
assetDate: "02/12/1986"
};
有没有人使用jQuery遇到过这类问题?
答案 0 :(得分:2)
没有对象的名称。有物,完全停止。有零个或多个变量引用给定对象,完全停止。你不能从一个对象转到引用它的变量,句号,新段落。
如果此“名称”是对象本身的重要属性,请将其包含在对象中(添加name
字段)或使用关联数组(也称为命名空间,引擎盖下的对象)名称“是关键,资产对象是值而不是数组。
答案 1 :(得分:1)
将资产对象作为属性存储在对象中(即使用关联数组而不是序数数组):
var assets = {
assetX: {
assetNumber: "TESTX",
assetDescription: "FLUX CAPACITOR",
assetManufacturer: "Honeywell",
assetCustomer: "MCFLY",
assetDate: "03/04/1956"
},
assetY: {
assetNumber: "C123Y",
assetDescription: "HOVERBOARD",
assetManufacturer: "GE",
assetCustomer: "MCFLY",
assetDate: "12/03/1945"
},
...
};
$.each(assets, function (name, asset) {
// name is the name (e.g. "assetX")
// asset is the object instance
});
答案 2 :(得分:0)
我不确定你想要展示什么。
但您可以只显示对象的各个部分:
//the assetArray will have varying number of elements in the future
var assetArray=[assetX,assetY,assetZ, assetB, assetA];
var testtest=(new String(assetArray));
alert(testtest);
$.each(assetArray,function(intIndex,objValue){
var test123=objValue.assetNumber; //etc
alert(test123);
});
答案 3 :(得分:0)
我建议使用常规JavaScript对象将对象名称与值匹配。