开发者

how to get name from array of objects

开发者 https://www.devze.com 2023-03-12 21:24 出处:网络
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]

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"开发者_如何学Go
};
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?


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):

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号