javascript对象/数组for循环来分配键

时间:2015-07-16 17:57:08

标签: javascript arrays

我有一个看起来像这样的对象:

var obj = {
    "cat" : [
        {val: 'res', string: "Residential & Single Family Homes"},
        {val: 'Dup', string: "Duplexes & Apartments"},
        {val: 'Con', string: "Condominiums"},
        {val: 'Lot', string: "Lots, Land & Farms"},
        {val: 'Com', string: "Commercial"},
        {val: 'Mob', string: "Mobile Homes"},
    ],
    "bdrms" : [
        {val: "1", string: "1 Bedroom"},
        {val: "2", string: "2 Bedrooms"},
        {val: "3", string: "3 Bedrooms"},
        {val: "4", string: "4 Bedrooms"},
        {val: "5", string: "5 Bedrooms"},
    ],
    "bthrms" : [
        {val: "1", string: "1 Bathroom"},
        {val: "1.1", string: "1 1/2 Bathrooms"},
        {val: "2", string: "2 Bathrooms"},
        {val: "2.1", string: "2 1/2 Bathrooms"},
        {val: "3", string: "3 Bathrooms"},
        {val: "3.1", string: "3 1/2 Bathrooms"},
        {val: "4", string: "4 Bathrooms"},
        {val: "4.1", string: "4 1/2 Bathrooms"},
        {val: "5", string: "5 Bathrooms"},
    ],
    "zip" : [

    ]
};

我想做的是动态生成一个邮政编码数组" zip"使用for循环。到目前为止我没有工作,因为我得到了一堆错误,如意外的令牌等......

我假设我完全做了这个,但这就是我试图做的事情:

"zip" : [
    for(i=43000; i<=45999;){
        {val: i, string: i},
        i++
    };
]

6 个答案:

答案 0 :(得分:4)

最简单的事情就是在对象初始化程序之后执行它:

for(i=43000; i<=45999;i++){
    obj.zip.push({val: i, string: i});
}

(请注意,我移动了i++,它没有业务存在于循环体内。)(另请注意,除非您在某处声明了i,否则它会成为{for的牺牲品。 3}}。)(;块后面没有"zip" : function() { var a = []; for(var i=43000; i<=45999;i++){ a.push({val: i, string: i}); } return a; }() 。:-))

如果它非常重要它是初始化程序的一部分(不太可能),你可以使用你立即调用的临时函数:

@Provider
public class RESTExMapper implements ExceptionMapper<Throwable>
{
    @Override
    public Response toResponse(Throwable ex)
    {
        //our application maps a not logged in exception to javax.ws.rs.core.Response.Status.UNAUTHORIZED in this Pair
        Pair<Integer, ObjectMap> ret = buildResponse( unwrap( ex));

        ResponseBuilder rb = Response.status( ret.left()).entity( ret.right()).type( "application/json");
        if( ret.left() == UNAUTHORIZED.getStatusCode())
            return rb.header( HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"YOUR SERVICE NAME\"").build();
        else
            return rb.build();
    }

......但我没有充分的理由。虽然我不确定为什么不......

答案 1 :(得分:2)

只需要一个空数组zip: [],然后使用循环将包含值的对象推入其中,确保var循环变量:

for (var i = 43000; i <= 45999; i++) {
    obj.zip.push({ val: i, string: i });
};

DEMO

答案 2 :(得分:1)

"zip" : [ <---- Having a loop inside an array will throw an error for(i=43000; i<=45999;){ {val: i, string: i}, i++ }; ]

假设看起来像

"zip": function () { //<--- Need to write up a function
    var arr = [];    // that will return an array instead
    for (var i = 43000; i <= 45999; i++) {
        var oo = {   // 
            val: i,
            string: i
        };
        arr.push(oo);
    };

    return arr;

}()

所以基本上它是一个计算数组并​​返回邮政编码的函数。

答案 3 :(得分:0)

var zip = [];
for (var i = 43000; i <= 45999; i++) {
    zip.push({val: i, string: i});
}
console.log(zip);

将输出包含所需对象的zip数组。

编辑:被Andy击败!

答案 4 :(得分:0)

尝试将其作为简单易读的解决方案:

"zip": getZipCodes()

function getZipCodes() {
    var codes = [];
    for(var i = 43000; i <= 45999; i++){
        codes.push({val: i, string: i});
    }

    return codes;
};

这样你就不会在创建后修改对象。

答案 5 :(得分:-2)

您无法在数组文字内部运行循环。这将有效:

   var obj = {
        "cat" : [
            {val: 'res', string: "Residential & Single Family Homes"},
            {val: 'Dup', string: "Duplexes & Apartments"},
            {val: 'Con', string: "Condominiums"},
            {val: 'Lot', string: "Lots, Land & Farms"},
            {val: 'Com', string: "Commercial"},
            {val: 'Mob', string: "Mobile Homes"},
        ],
        "bdrms" : [
            {val: "1", string: "1 Bedroom"},
            {val: "2", string: "2 Bedrooms"},
            {val: "3", string: "3 Bedrooms"},
            {val: "4", string: "4 Bedrooms"},
            {val: "5", string: "5 Bedrooms"},
        ],
        "bthrms" : [
            {val: "1", string: "1 Bathroom"},
            {val: "1.1", string: "1 1/2 Bathrooms"},
            {val: "2", string: "2 Bathrooms"},
            {val: "2.1", string: "2 1/2 Bathrooms"},
            {val: "3", string: "3 Bathrooms"},
            {val: "3.1", string: "3 1/2 Bathrooms"},
            {val: "4", string: "4 Bathrooms"},
            {val: "4.1", string: "4 1/2 Bathrooms"},
            {val: "5", string: "5 Bathrooms"},
        ],
        "zip" : [

        ]
    },
    zips = obj.zip,
    i,
    counter = 0,
    j;
    for(i=43000; i<=45999; i++){
        zips[counter] = { val: i, string: i };
        counter++
    };