如何从对象数组中获取名称
我有一个数组表示法中的 对象 数组。这个数组每次都会不同。
我希望能够获取该数组中元素的名称。例如,我遇到的麻烦是返回 [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 解决此类问题的经验?
I have an array of objects in array notation. This array is going to be different every time.
I wish to be able to get the name of the elements in this array. The trouble I am having is that [object] is returned instead of returning assetA, for example. I know this is because assetA is an object itself.
An example of the code I have been using to test this is..
//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);
});
and the list of test assets are..
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"
};
Does anyone have experience with this kind of issue using jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不存在物体的名称这样的东西。有物体,句号。有零个或多个引用给定对象的变量,句号。你不能从一个对象转到引用它的变量,句号,新段落。
如果此“名称”是对象本身的重要属性,请将其包含在对象中(添加
name
字段)或使用关联数组(也称为名称空间,幕后的对象)与此“ name”是键,资产对象是值而不是数组。There is no such thing as the name of an object. There are objects, full stop. There are zero or more variables which refer to a given object, full stop. You can't go from an object to the variables that reference it, full stop, new paragraph.
If this "name" is an important property of the object itself, include it in the object (add a
name
field) or use an associative array (aka namespace, an object under the hood) with this "name" being the key and the asset objects being the values instead of the array.将资产对象作为属性存储在对象中(即使用关联数组而不是序数数组):
Store your asset objects in an object as properties (i.e. use an associative array instead of an ordinal array):
我不确定你想显示什么。
但您可以只显示对象的各个部分:
I am not sure what you want to display.
But you can just display the individual parts of the Object:
我建议使用常规 JavaScript 对象来将对象名称与值进行匹配。
I'd suggest using a regular JavaScript object to match object names to values.