自动递增JS对象数组

时间:2016-02-19 20:50:27

标签: javascript jquery arrays javascript-objects

我有点难过,可能会忽略一些事情。 基本上我有一个JS对象数组:

var defaults = {
    pointers : {
        pointer1 : {
            top : '0px',
            left : '75px',
        },
        pointer2 : {
            top : '310px',
            left : '170px',
        },
        pointer3 : {
            top : '50px',
            left : '80px'
        },
        pointer4 : {
            top : '0px',
            left : '130px',
        },
        pointer5 : {
            top : '310px',
            left : '205px',
        },
        pointer6 : {
            top : '50px',
            left : '90px'
        }
    }
};

如果我手动调用它们添加到<li>,类pointer-circle ,则可以正常使用

$('.pointer-circle:eq(1)').css({
    top : defaults.pointers.pointer1.top,
    left : defaults.pointers.pointer1.left
});

但是不是手动输出每个指针而是希望它们通过循环:

var i = 1;
for (i in defaults.pointers) {
    $('.pointer-circle:eq(' + i + ')').css({
        top : defaults.pointers.pointer[i].top,
        left : defaults.pointers.pointer[i].left
    });
}

这不起作用。在控制台日志中,我收到此错误:

  

未捕获的TypeError:无法读取未定义的属性“pointer1”

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

i.top直接指对象。你想要for。您正在将标准for-in循环与$('.pointer-circle:eq(' + i + ')')循环混淆。当然,这意味着listDF 不是您认为的

答案 1 :(得分:2)

这就是你要去的地方:

       var model = students.Join(scholarshipsRequests,
           x => x.ScholarshipId ?? 0, y => y.ScholarshipId,
           (x, y) => x != null && y != null ? (new AdminScholarshipReqViewModel
           {
               StudentId = x.StudentId,
               ScholarshipId = y.ScholarshipId,
               Tag = x.Tag,
               FullName = x.FullName,
               Division = x.Career.Division,
               Company = y.Company,
               Comments = y.Comments,
               CreationDate = y.CreationDate,
               ClassificationDescription = y.Classifications.Description,
           }) : new AdminScholarshipReqViewModel()
           {
               StudentId = 0,
               ScholarshipId = 0,
               Tag = "",
               FullName = "",
               Division = "",
               Company = "",
               Comments = "",
               CreationDate = DateTime.Now,
               ClassificationDescription = "",

           }).ToPagedList(pageNumber, pageSize);
            if (model != null) return View(model);

(另见this Fiddle