在 Javascript 中循环数组时检测值何时发生变化?

发布于 2024-12-03 08:03:26 字数 1437 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

人疚 2024-12-10 08:03:26
var events = [/* as in the question */];
var groupedEvents = {};

for (var i = 0; i < events.length; ++i) {
    var time = events[i].section;
    var action = events[i].title;

    if (!groupedEvents.hasOwnProperty(time)) {
         groupedEvents[time] = [];
    }
    groupedEvents[time].push(action);
}

现在groupedEvents包含:

{
     "An Hour before Dinner": ["Cook peas", "Take pie out"],
     "As guests arrive": ["Mix Drinks"],
     "Just before dessert": ["Reheat at 325 about 20 minutes.", "Cut and serve."]
}
var events = [/* as in the question */];
var groupedEvents = {};

for (var i = 0; i < events.length; ++i) {
    var time = events[i].section;
    var action = events[i].title;

    if (!groupedEvents.hasOwnProperty(time)) {
         groupedEvents[time] = [];
    }
    groupedEvents[time].push(action);
}

Now groupedEvents contains:

{
     "An Hour before Dinner": ["Cook peas", "Take pie out"],
     "As guests arrive": ["Mix Drinks"],
     "Just before dessert": ["Reheat at 325 about 20 minutes.", "Cut and serve."]
}
疯到世界奔溃 2024-12-10 08:03:26

这应该可以解决问题:

var arr = [
    {section:"An Hour before Dinner", title:"Cook peas"},
    {section:"An Hour before Dinner", title:"Take pie out"},
    {section:"As guests arrive", title:"Mix drinks"},
    {section:"Just Before Dessert", title:"Reheat at 325 about 20 minutes."},
    {section:"Just Before Dessert", title:"Cut and serve."}
];
var s = null;

var html = "";
for (var i = 0, l = arr.length; i<l; i++) {
    var r = arr[i];
    if (r.section != s) {
        // New section
        s = r.section;
        html += "<h1>"+s+"</h1>"
    } else {
        // Continue old section
    }
    html += "<p>"+r.title+"</p>";
}

var div = document.createElement('div');
div.innerHTML = html;
document.body.appendChild(div);

This should do the trick:

var arr = [
    {section:"An Hour before Dinner", title:"Cook peas"},
    {section:"An Hour before Dinner", title:"Take pie out"},
    {section:"As guests arrive", title:"Mix drinks"},
    {section:"Just Before Dessert", title:"Reheat at 325 about 20 minutes."},
    {section:"Just Before Dessert", title:"Cut and serve."}
];
var s = null;

var html = "";
for (var i = 0, l = arr.length; i<l; i++) {
    var r = arr[i];
    if (r.section != s) {
        // New section
        s = r.section;
        html += "<h1>"+s+"</h1>"
    } else {
        // Continue old section
    }
    html += "<p>"+r.title+"</p>";
}

var div = document.createElement('div');
div.innerHTML = html;
document.body.appendChild(div);
土豪 2024-12-10 08:03:26

假设它们已正确排序,并且位于数组“arr”中:

var pre = '';
for (var i = 0; i < arr.length; i++) {
   if (arr[i].section != pre) displayHeading(arr[i]);
   pre = arr[i];
   displayContent(arr[i]);
}

Assuming they are correctly sorted, and in an array "arr" :

var pre = '';
for (var i = 0; i < arr.length; i++) {
   if (arr[i].section != pre) displayHeading(arr[i]);
   pre = arr[i];
   displayContent(arr[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文