如何从对象数组中获取名称

发布于 2024-11-15 03:24:36 字数 1482 浏览 8 评论 0原文

我有一个数组表示法中的 对象 数组。这个数组每次都会不同。

我希望能够获取该数组中元素的名称。例如,我遇到的麻烦是返回 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骷髅 2024-11-22 03:24:36

不存在物体的名称这样的东西。有物体,句号。有零个或多个引用给定对象的变量,句号。你不能从一个对象转到引用它的变量,句号,新段落。

如果此“名称”是对象本身的重要属性,请将其包含在对象中(添加 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.

梦归所梦 2024-11-22 03:24:36

将资产对象作为属性存储在对象中(即使用关联数组而不是序数数组):

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
});

Store your asset objects in an object as properties (i.e. use an associative array instead of an ordinal array):

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
});
千仐 2024-11-22 03:24:36

我不确定你想显示什么。

但您可以只显示对象的各个部分:

//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);
});

I am not sure what you want to display.

But you can just display the individual parts of the Object:

//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);
});
飘逸的'云 2024-11-22 03:24:36

我建议使用常规 JavaScript 对象来将对象名称与值进行匹配。

I'd suggest using a regular JavaScript object to match object names to values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文